Updated

Buster Posey pounded a two-run home run and scored twice to lead the San Francisco Giants to a 4-3 triumph over the Arizona Diamondbacks Saturday in the second contest of a three-game series at AT&T Park.

Pablo Sandoval collected two hits and drove in a run, while Jeff Francoeur was credited with the other RBI for the Giants, who have won five of their last six games.

Matt Cain (6-6) had gone 0-3 over his last five starts, but held the D'backs to two runs on four hits and four walks over five-plus frames to earn the win.

"I thought Matt looked good," Giants manager Bruce Bochy said of his starter. "He threw the ball well tonight. I thought overall a real nice effort and won the ballgame for us."

Six San Francisco relievers combined to keep the visitors off the board from there before Sergio Romo secured his 23rd save of the season, despite giving up a run in the bottom of the ninth.

Arizona, meanwhile, has dropped three straight games to see its lead atop the NL West shrink to just one-half game over the Los Angeles Dodgers, who downed the Washington Nationals in 10 innings earlier Saturday.

Martin Prado plated a pair of runs and scored another to account for Arizona's offense. Starter Wade Miley (6-8) gave up two runs on four hits over four innings to absorb the loss.

"It's obviously not the way we wanted to start the second half, but we still have 65 more games," Miley said of his team dropping back-to-back games coming out of the All-Star break. "It's really no need to hit the panic button right now. We'll be fine."

The Giants jumped on Miley in the first, as Marco Scutaro worked a one-out walk, moved to second on a wild pitch, and scored two batters later when Sandoval smacked a double to left.

Posey, Sandoval and Hunter Pence started the fourth with three consecutive singles before Francoeur earned a free pass to make it 2-0.

Miley then uncorked a wild pitch to Tony Abreu, and Sandoval tried to score from third on the play, but catcher Miguel Montero got to the ball quickly and fired to Miley, who applied the tag on Sandoval to keep the margin at 2-0.

Abreu grounded Miley's next offering to third, where Prado scooped it up before firing a strike to the plate to nab Pence for the second out. Miley struck out Guillermo Quiroz to end the frame before Prado led off the fifth with a walk.

After Cliff Pennington punched a one-out single to right, pinch-hitter Eric Chavez, batting for Miley, smacked a grounder to second. Scutaro fielded the ball and fired to second to get the runner, but Abreu's relay throw to first sailed wide, allowing Prado to score to bring the D'backs within 2-1.

Posey picked up his battery mate in the home half when he launched a two-out, two-run blast to straightaway center field to make it 4-1.

Arizona chased Cain from the contest in the sixth after Aaron Hill and Montero stroked consecutive singles to put men on the corners with no outs. George Kontos entered from the bullpen and served up a single to Prado that plated Hill to trim Arizona's deficit to 4-2.

The D'backs rallied in the ninth, as Goldschmidt scorched a leadoff single that deflected off Romo and trickled toward third. Romo bounced back by retiring the next two hitters, but Prado came through with a looping single down the right-field line to bring the visitors within one.

Cody Ross stepped to the plate with the tying run on first, but Romo fanned his former teammate on three pitches to end the game.

Game Notes

Giants reliever Jeremy Affeldt left the game in the sixth due to a left groin strain ... Miley had yielded three earned runs or fewer in eight straight starts coming into the game ... Arizona went 2-for-12 with runners in scoring position and left 13 runners on base ... Ross and Romo were teammates during the Giants' run to the 2010 World Series title.