SAN FRANCISCO -- Buster Posey drilled a two-run homer in the fifth inning as the San Francisco Giants outlasted the Arizona Diamondbacks 4-3 on Saturday night in a wild game that featured a little of everything, including two tag-outs at home plate during a single at-bat.
The Giants' second win over the National League West leader in two days narrowed Arizona's lead over the second-place Los Angeles Dodgers to a half-game, with defending champion San Francisco now just 4 1/2 back in fourth.
Matt Cain (6-6) posted his first victory in more than a month, but seven Giants relievers combined to outpitch him, protecting a narrow lead while holding the Diamondbacks to one run in the final four innings.
The win was San Francisco's seventh in its last eight matchups with the Diamondbacks. The Giants hold an 8-3 lead in the season series.
Posey's home run, his 14th of the season, came off Arizona reliever Josh Collmenter with two outs in the fifth, scoring Andres Torres ahead of him and increasing the Giants' lead to 4-1. Posey, who also singled and scored in the fourth inning, leads the Giants with 59 RBIs.
Cain never got an out in the sixth, allowing singles by Aaron Hill and Miguel Montero before right-hander George Kontos replaced him.
Cain, who failed to get out of the first inning in his previous start and had been 0-3 since June 13, held the Diamondbacks to two runs despite surrendering four hits and four walks in five innings. He struck out four.
The Diamondbacks scored in the sixth when Martin Prado greeted Kontos with an RBI single. But Kontos, Jeremy Affeldt and Jose Mijares combined to get out of the inning with no further damage, even though Affeldt had to leave mid-batter with a strained left groin.
Santiago Casilla, Sandy Rosario, Javier Lopez and Sergio Romo completed the seven-man bullpen effort as the San Francisco relievers permitted five hits and two walks but just one run.
The run came off Romo in the ninth on a two-out single by Prado, who fittingly was stranded as the Giants closer picked up his second save in two nights and his 22nd of the season.
The Diamondbacks, who had gone 1-for-17 with runners in scoring position in their previous four games, made the least of nine hits and six walks, stranding 13 base runners, including the bases loaded in the fifth and sixth innings. Arizona batters went just 2-for-13 with runners in scoring position.
Left-hander Wade Miley (6-8) took the loss for the Diamondbacks, leaving in the fifth inning with Arizona down 2-0.
Five Arizona pitchers combined to give up eight hits and five walks but stranded seven Giants base runners.
Posey and Pablo Sandoval, who had an RBI single in the first inning, combined for half of the Giants' eight hits.
The Giants nearly knocked out Miley in the fourth, but his own athletic play at home plate helped quell what could have been a game-breaking uprising.
After the Giants loaded the bases on singles by Posey, Sandoval and Hunter Pence, Miley walked Jeff Francoeur to force in a run that increased the San Francisco lead to 2-0.
Miley sailed his next pitch over the head of Montero. But with Sandoval rumbling home, Montero retrieved the ball at the backstop and threw a strike to Miley, who in turn slapped down a tag just in time to get the sliding Sandoval.
Tony Abreu then grounded to Prado, whose throw from third base to the plate was in plenty of time to get the out. Pence went in standing up and crashed into Montero, but the Arizona catcher held onto the ball for the Diamondbacks' second consecutive tag-out at the plate.
When Miley struck out Guillermo Quiroz, the Giants had only one run to show for three hits, a walk and two mad dashes for the plate.
Miley left for a pinch hitter after the exhausting inning and Diamondbacks manager Kirk Gibson's strategy produced his team's first run. Prado raced home from second when Abreu, the Giants' shortstop, threw wildly to first base in an attempt to turn a double play on pinch-hitter Eric Chavez's ground ball.
The Diamondbacks went on to load the bases after Cain walked Adam Eaton and Gerardo Parra, but RBI leader Paul Goldschmidt grounded to third to strand all three and leave Arizona still trailing 2-1.
Miley, who has never given up more than three runs in any of his four career starts against the Giants, surrendered two runs, four hits and two walks in his four innings. He struck out four.
The Giants took advantage of a four-pitch walk to their second batter of the game, Marco Scutaro, to score first. Sandoval delivered the first-inning run with a two-out double into the left-field corner.
NOTES: The Giants scored in the first inning for the second consecutive game. The Giants did all their scoring in the first in Friday's 2-0 win, the first time since 2006 that they won a game when scoring only in the first inning. ... Diamondbacks RHP Brandon McCarthy is scheduled to make an injury-rehab start for Triple-A Reno on Sunday. McCarthy has been on the disabled list since May 31 with a shoulder problem, a seven-week layoff that included a seizure that was related to getting hit in the head by a line drive last season. ... Giants RHP Ryan Vogelsong threw 75 pitches before the game -- 35 in live batting practice, 40 in a bullpen session -- and was deemed healthy enough to make an injury-rehab start on Tuesday in the Arizona Fall League. Vogelsong broke bones in and below his right pinkie finger on May 20 when hit by a pitch. ... Giants SS Brandon Crawford, 0-for-his-last-18 against left-handed pitchers, was not in the starting lineup in part because of a swollen left foot that was spiked at first base by Diamondbacks RHP Heath Bell late in Friday's game.