Hairston hit reliever Vinnie Chulk's first pitch 418 feet into the left-field seats with two runners on to give the Diamondbacks a 5-4 lead in the seventh inning.
It was the 14th homer of Hairston career, leaving him just 728 shy of Bonds. Hairston homered for the first time since 2004.
"It's been a while since I've done something like that, and it feels great," Hairston said. "Usually I take a pitch coming off the bench, but that hasn't been working. So I just decided to switch it up and try something new, and it worked out."
Everything seems to be working out for the Diamondbacks, who won their fourth straight. They slipped past the Giants into second place in the NL West, a half-game behind the Los Angeles Dodgers.
The modest win streak started on Wednesday night, when the Diamondbacks overcame 16 strikeouts by San Diego's Jake Peavy and won on Stephen Drew's ninth-inning homer off Trevor Hoffman.
Drew spent much of last year at Triple-A Tucson, as did Hairston and Dustin Nippert (1-0), who earned the victory with two scoreless innings.
"That's how it worked last year in Triple-A," Nippert said. "We would go into the seventh inning, we'd be losing and nobody would even be worried about it."
The difference is that they are playing San Francisco now, not Salt Lake.
"This is definitely different than Triple-A, but it's the same attitude," Nippert said.
The Giants dropped their second straight after winning eight in a row. While they're happy to see Bonds crushing the ball -- he has eight homers in 58 at-bats -- they need others to produce.
"He got us on the board, but we had some other opportunities," manager Bruce Bochy said. "We need to cash in and get a hit with men on base."
The Giants' last chance came in the ninth, when Pedro Feliz led off with a bloop single to right field off Arizona closer Jose Valverde. Randy Winn reached on a fielder's choice grounder and took second on a wild pitch.
After hitting a line drive that landed inches foul down the right-field line, pinch-hitter Ray Durham flied out to deep center. Valverde got Dave Roberts to ground to short to earn his 10th save in 11 chances.
That came as a relief to Melvin, who worried that his decision to challenge Bonds would cost Arizona the game.
With a runner at second and two outs in the first inning, Melvin decided not to walk Bonds, as he did three times in the series opener Friday night. Bonds hit a 1-1 pitch from right-hander Edgar Gonzalez 428 feet into the concourse in right-center field to give the Giants a quick 2-0 lead.
"That's not my crowning moment right there," Melvin said. "At some point in time, especially early in a game, you want to try to maybe set a precedence. We're not going to walk this guy every time. We're not scared to death of him. But the minute I thought about that, and then the ball leaves the ballpark, you want to crawl into a hole.
"I tried to go a little bit against the grain and it didn't work," Melvin said. "I learned my lesson."
It was Bonds' eighth home run of the season and his third off Gonzalez.
Melvin changed his mind in the fifth, walking Bonds with a runner on second. But by then the Giants led 4-0.
He was just as dominant Saturday night, although he struggled with his control at times, walking two men in the second and fifth innings.
"I ran myself out of the game because of the pitch count," said Cain, who threw 99 pitches. "I got myself deep into counts with a lot of guys and it hurt me."
Cain allowed one hit and one run in six innings. He walked four and struck out two.
Chulk (0-1) and Jack Taschner couldn't protect a 4-1 lead. In the seventh, Taschner gave up a single to Chad Tracy, a walk to Carlos Quentin and an RBI single to Miguel Montero, which cut the Giants' lead to 4-2.
Chulk relieved Taschner and gave up Hairston's homer on his first pitch.
"It was a sinker that stayed up," Chulk said. "I was just trying to get a ground ball and I threw it in the spot, but it didn't sink."
- Bonds has 19 homers at 10-year-old Chase Field, most by a visiting player.
- Arizona 2B Orlando Hudson received his 2006 NL Gold Glove award before the game. Hudson, who also won a Gold Glove with Toronto in 2005, is the sixth infielder to win the award in both leagues.