SAN FRANCISCO -- Down 6 1/2 games at the All-Star break, the San Francisco Giants knew they needed to make up ground in a hurry in the National League West race.
They got that opportunity with the first-place Arizona Diamondbacks coming to town, and they've taken advantage.
Buster Posey drilled a two-run homer in the fifth inning as the Giants outlasted the 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 NL 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.
The two teams meet in the series finale on Sunday, with the Giants seeking to get even closer while perhaps knocking the Diamondbacks out of first place.
"At this point, all the focus is on the game of the day regardless of what happened the day before," said Posey, whose two RBIs increased his team-leading total to 59. "That's a good first two games of the series. Hopefully, we can keep it going."
Matt Cain (6-6) posted his first victory in more than a month, but seven Giants relievers combined to out-pitch 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.
"I took it easy over the All-Star break. It's just nice to have rest," said Cain, who had not pitched since a disastrous outing against the New York Mets on July 10. "I think all the guys wanted to get to the All-Star break. It was good for everybody."
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 also singled and scored the Giants' second run one inning earlier.
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.
"They've always been big for us," Cain said of the Giants' bullpen crew. "To come in today and shut them down for four innings, that's not easy."
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-12 with runners in scoring position.
Paul Goldschmidt, the Diamondbacks' leader in RBIs with 77, failed twice with runners in scoring position. He singled and scored Arizona's ninth-inning run.
"Goldy's been carrying us all year," Arizona manager Kirk Gibson said. "We need somebody to pick him up.
"We had opportunities. We didn't capitalize on it."
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. Each had a single in a wacky fourth inning that featured two tag-outs at home plate.
After he had 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 his catcher, 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 Giant.
Later in the at-bat, Tony Abreu grounded to third baseman Prado, whose throw to the plate was in plenty of time to get the out. The Giants' Hunter 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.
NOTES: The ninth-inning run off Romo snapped his string of 29 consecutive scoreless innings against the Diamondbacks. ... 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.