Giants hold off Diamondbacks 4-3

San Francisco Giants' Hunter Pence tries unsuccessfully to knock the ball out of the mitt of Arizona Diamondbacks catcher Miguel Montero in an attempt to score in the fourth inning at AT&T Park in San Francisco on Saturday, July 20, 2013. (Jim Gensheimer/Bay Area News Group)

SAN FRANCISCO -- It wasn't easy, but the Giants managed to make up another game in the National League West on Saturday night.

The first-place Arizona Diamondbacks put at least two runners on in each of the final five innings, but Matt Cain and seven Giants relievers held on for a 4-3 victory at AT&T Park that got the club within 4½ games of the division lead. The bullpen made a winner of Cain, who pitched into the sixth after totaling just three innings in his previous two starts.

The Giants are 29-16 when they score first, and for the second straight night they scored in the first inning. Marco Scutaro walked and scored on Pablo Sandoval's two-out double.

The Giants added another run in the fourth when Buster Posey, Sandoval and Hunter Pence hit consecutive singles and Jeff Francoeur drew a bases-loaded walk with no outs. The lineup failed to tack on, with Sandoval getting thrown out at home on a wild pitch and Pence getting thrown out at the plate on a grounder to third.

Still trailing by just two runs, the Diamondbacks got one back in the fifth. With two runners on, pinch-hitter Eric Chavez hit a grounder to second. The Giants got an out at second, but a run scored when shortstop Tony Abreu's throw to first bounced into the seats. The Diamondbacks loaded the bases, but Cain got Paul Goldschmidt to ground out to end the inning.

Posey widened the gap in the bottom of the inning, hitting a two-out, two-run homer over the center-field wall. The blast was Posey's 14th of the season.

The Giants bullpen had stirred in the fifth inning, and Cain was removed in the sixth after Aaron Hill and Miguel Montero led off with back-to-back singles. Martin Prado's single off George Kontos scored a run, but the more serious issue came three batters later. Jeremy Affeldt was summoned to get the Giants out of the inning but had to leave the game after straining his left groin. Affeldt immediately grabbed at his leg after throwing a pitch to A.J. Pollock.

Jose Mijares entered and got the Giants out of the jam by striking out Adam Eaton with the bases loaded. The Diamondbacks put two runners on in the seventh and in the eighth but failed to score both times.

Sergio Romo gave up a run in the ninth but struck out Cody Ross with a runner on to clinch the win.

Ryan Vogelsong (fractured right pinkie) finally faced hitters Saturday. Well, kind of. Vogelsong threw 35 pitches off the mound at AT&T Park, the majority of which were thrown with coaches Roberto Kelly and Shawon Dunston in the batter's box.

"I don't (judge) too much off the swings anyway," Vogelsong said. "Especially with those two guys."

Health was all that ultimately mattered during the short session, and Vogelsong was pleased with how he held up.

"I came out of it healthy," he said. "I took out of it what I needed to. The hand feels good and the arm feels good. I threw some pitches I wasn't happy with, but a lot that I was happy with."

Vogelsong said he is back in spring training mode in terms of workload. If there are no setbacks in the next couple of days, the right-hander will fly to Arizona to throw in a rookie league game. He would then make an appearance with the San Jose Giants before joining Double-A Richmond.

Manager Bruce Bochy watched Saturday's session closely and was pleased with Vogelsong's performance. Bochy said the Giants continue to look at Aug. 9 or 10 as possible return dates for Vogelsong.

Shortstop Brandon Crawford had a wrap on his left foot and said he was sore a day after getting stepped on by Heath Bell during a play at first base. Crawford was out of the starting lineup but came in on a double-switch in the sixth inning.

Utility infielder Joaquin Arias (appendectomy) hit off a tee and did some light fielding work on Saturday. Arias likely will return sometime next week.