SAN FRANCISCO -- Lasting five innings is not usually an accomplishment for a workhorse such as Matt Cain.
Considering the way his past two starts went, that had to feel pretty satisfying.
Cain bounced back from those abbreviated rough outings to win for the first time in more than a month as the San Francisco Giants beat the first-place Arizona Diamondbacks for a second straight night, winning 4-3 on Saturday.
Buster Posey hit a two-run homer to lead the offense but the bigger story was Cain (6-6). He allowed two runs in five-plus innings after failing to make it out of the third inning his past two starts to win for the first time since June 13 at Pittsburgh.
"You go out there and you can't even make it through an inning, that's never any fun," Cain said. "But you kind of have to put it past you. As a starter, that's beaten into you that you want to go out and throw seven innings. You feel like you let your guys down. That's the biggest thing. You feel like you put your team in a rut."
While Cain was unable to retire a batter in the sixth, the work from seven relievers helped make sure he got the win that helped the World Series champion Giants cut their deficit in the NL West to 4½ games behind Arizona.
The Diamondbacks' lead over the Los Angeles Dodgers fell to a half-game as Wade Miley (6-8) lasted just four innings and All-Star Paul Goldschmidt failed to deliver in a key spot for the second straight game.
"This division, nobody seems to want to run away with it," Arizona outfielder Cody Ross said. "It's there for the taking. As bad as we've been playing, we're still a half game up."
Cain was coming off two disastrous starts before the All-Star break. He gave up eight runs and was knocked out in the third inning of a loss to the Dodgers on July 5 and retired only two batters and gave up three runs to the Mets five days later in his shortest career start.
But Cain looked sharp early, striking out two batters in the first.
"He felt great, he looked great," manager Bruce Bochy said. "He had good stuff tonight. Overall, it was a really nice effort and it won the ballgame for us."
Cain didn't allow a run until the fifth when shortstop Tony Abreu threw the ball into the stands behind first while trying to complete a potential inning-ending double play.
Cain escaped further damage when he got Goldschmidt to ground out with the bases loaded to end the inning. It was the second straight game that the Giants got their nemesis to ground out with the bases loaded. Goldschmidt has the most homers and RBIs against San Francisco over the past three seasons.
"I don't think they're exploiting anything," manager Kirk Gibson said. "They're pitching him good. Goldy's been carrying us all year. It's part of the game, those things happen. They're fortunate to get through him."
Cain was replaced after allowing back-to-back singles to Aaron Hill and Miguel Montero to open the sixth. Martin Prado followed with an RBI single off George Kontos to cut San Francisco's lead to 4-2.
Kontos, Jeremy Affeldt and Jose Mijares each retired a batter to escape the inning with no further damage on the scoreboard. But Affeldt left the game with a strained left groin when he came up lame on a pitch to pinch-hitter A.J. Pollock. Affeldt underwent an MRI to determine the extent of the injury.
"That's never a good area," Bochy said. "It's a possible DL."
San Francisco used seven relievers to get through the final four innings Sergio Romo pitched the ninth for his 23rd save in 26 chances. Romo allowed a two-out RBI single to Prado, ending a streak of 29 consecutive scoreless outings against Arizona before striking out Ross to end it.
The Giants gave Cain an early lead to work with when Pablo Sandoval drove in Marco Scutaro with a two-out double in the first inning and appeared to be on the verge of breaking the game open in the fourth when they had the bases loaded and one run in before Miley escaped the jam.
Sandoval was thrown out trying to score from third on a pitch to the screen and Pence was thrown out on Abreu's grounder to Prado at third. Miley then struck out Guillermo Quiroz to keep the score 2-0.
Arizona has lost 12 of 15 road games. ... Giants RHP Ryan Vogelsong (broken finger) threw 33 pitches in a simulated game and could be ready to begin a rehab assignment Tuesday in the Arizona Rookie League. ... All-Star Madison Bumgarner (10-5) starts for the Giants in the series finale on Sunday against Randall Delgado (1-3).