Buster Posey had a two-run home run in the fifth to help the San Francisco Giants beat the Arizona Diamondbacks 3-2 on Saturday night.

Barry Zito scattered six hits and one run over 6 2-3 innings for the Giants, who have won four straight and 22 of their past 32. Zito (12-8) walked one and struck out four. Sergio Romo pitched the final 1 1-3 for his 11th save.

Justin Upton hit his 14th home run for the Diamondbacks, who have scored four runs or fewer in 12 straight home games to match a franchise record.

Arizona starter Wade Miley (15-10) allowed three runs on eight hits and a walk over seven innings.

Posey broke a 1-all tie in the fifth with a two-out home run two rows deep into the right-field stands. The All-Star catcher is hitting .390 in 55 games since the All-Star break with 12 homers and 49 RBIs.

Zito's outing was his longest since an eight-inning stint on August 23. He gave way to Santiago Casilla after surrendering a two-out ground rule double to left by John McDonald.

The Diamondbacks cut the lead to 3-2 in the bottom of the eighth. Aaron Hill walked, went to second on a ground out and scored on Paul Goldschmidt's single off Romo, the Giants' third pitcher of the inning.

San Francisco took a 1-0 lead two batters into the game when Angel Pagan tripled off the wall in left-center field and scored on a grounder to short by Marco Scutaro.

Upton tied the game in the bottom of the fourth with a solo shot to left-center field, his first homer in eight days and only his fifth against left-handed pitching this season.

Miley pitched into trouble in the fourth, loading the bases with a two-out walk to Hunter Pence, but retired Joaquin Arias on a fly ball to right to end the inning.

NOTES: Miley threw 116 pitches, the third time he has reached that mark this season. In both previous games, he earned the victory. . Pagan's first-inning triple was his 13th of the season, a new San Francisco Giants' record. Willie Mays holds the franchise's overall mark with 20 triples in 1957. . Goldschmidt was caught stealing in the fourth for only the third time in 19 attempts. . Arizona OF Tyler Graham made his major league debut in the ninth when he pinch ran for Miguel Montero. Two pitches later, Posey threw him out trying to steal second with a throw from his knees. ... The Diamondbacks opened the retractable roof mid-game for the second time this season and first since a May 13 contest against the Giants. . RHP Ryan Vogelsong (12-8) will take the mound for the Giants in Sunday's series finale against Diamondbacks LHP Patrick Corbin (5-7). Vogelsong is 2-3 in his past six starts and has allowed 28 runs on 43 hits in 26 1-3 innings for a 9.57 ERA. Corbin also has struggled of late, posting an 0-3 record and 6.00 ERA in his past four starts. . The Diamondbacks staged their first-ever alumni contest after the game.