PHOENIX (AP) -- The San Francisco Giants had been giving up homers in bunches, failing to get clutch hits and watching the losses pile up after a strong start to the season.
Even for a team that's won two of the previous three World Series titles, it was a tough stretch to take.
Brandon Belt hit a two-run single in the eighth inning and the Giants fought back after allowing three homers in the fourth to beat the Arizona Diamondbacks 6-4 Monday night and end a five-game losing streak.
"A much-needed win," Giants manager Bruce Bochy said. "The boys battled hard. You have a lead, they come back to take the lead and that can be deflating. But they kept grinding."
San Francisco scored three runs off Ian Kennedy the first two innings, including a solo homer by Belt in the second. Arizona hit three homers while scoring four runs off Matt Cain to go on top, but the Giants fought back in the eighth against Brad Ziegler (1-1).
Nick Noonan, a replacement after Pablo Sandoval left with discomfort in his right arm, got it started with a leadoff double. Ziegler then loaded the bases with a pair of walks and Belt came through with the hit the Giants had been waiting for, lining a two-run single up the middle to break a 4-all tie.
Marco Scutaro sparked San Francisco from the No. 2 spot in the lineup with three hits and two runs. Jean Machi (1-0) came in for Cain and pitched a perfect seventh inning for his first major league win. Sergio Romo closed it out in the ninth for his ninth save.
This was just what the Giants needed.
"It's been a tough stretch for us here lately," Belt said. "But I don't think our attitude really changed coming into this game. We knew we had a team that could win and that's what we did."
The Diamondbacks had their chances.
Arizona overcame Kennedy's shaky start with three home runs off Cain in the fourth inning: a two-run shot by Jason Kubel and solo homers by Eric Chavez and Martin Prado.
That was it for the Diamondbacks, though. They had two hits the rest of the way and were 0 for 5 with runners in scoring position to drop out of first place in the NL West after moving to the top the day before.
"We hit the ball out of the ballpark, we tied it up and they didn't die," Arizona manager Kirk Gibson said. "It went down to the end but it just wasn't our night."
A three-time All-Star, Cain finally got some run support, but continued to struggle with the long ball.
The Giants scored three runs in his first five starts and matched that in the first two innings.
San Francisco scored a pair of runs off Kennedy in the first inning on a sacrifice fly by Hunter Pence and Gregor Blanco's run-scoring single. Belt led off the second with a drive to right to make it 3-0.
Cain allowed six homers in his previous three starts, and the balls kept flying in the fourth against Arizona.
He walked Cody Ross to lead off the inning and Kubel sent the next pitch over the wall with a towering shot to right. Chavez tied it two pitches later with a line-drive solo shot that cleared the wall in left.
Prado hit a third homer in the inning - a first for Arizona since Aug. 11, 2010, at Milwaukee - when he drove a 1-2 pitch that Cain left over the plate. The ball landed near where Chavez's homer went.
Once cruising, Cain found himself trailing 4-3 after Arizona's homer barrage.
Cain allowed five hits, struck out six and walked four in six innings. He didn't figure in the decision, though, after Sandoval hit a tying single in the fifth.
San Francisco's pitchers have allowed 28 homers in 26 games.
"It's fair to say he didn't have his best stuff, but he was battling well," said Bochy, who was ejected in the fifth inning after arguing a double-play call at first base. "That one inning, they took advantage of some pitches and in this ballpark, it can happen fast, which it did. But he regrouped and got us through six."
Kennedy also battled back after his early troubles. He allowed four runs and seven hits in seven innings to remain winless in his five starts since opening day.
"It was one of those days you feel right away that you don't have your best stuff and it is going to be a battle," Kennedy said. "Even after the first hitter, I knew I wasn't as sharp. I kept them close. I know Matt is a real good pitcher, but I believe in our offense so I wanted to keep it close as long as possible and we would score some runs."
NOTES: Arizona RHP Daniel Hudson threw about 30 pitches in a simulated game and another 20 during warmups in the latest step toward his return from Tommy John surgery last season. The Diamondbacks hope to have him back before the All-Star break. ... Arizona OF Gerardo Parra went 0 for 5 to end a 13-game hitting streak. ... Giants C Buster Posey extended his hitting streak to 10 games. ... Giants LHP Madison Bumgarner will be looking to start the season 4-0 when he faces the Diamondbacks on Tuesday. He received a no-decision in his last start, allowing a run in 7 2-3 innings against Arizona on Wednesday. ... Arizona RHP Trevor Cahill will be trying for his second straight win against the Giants after opening the season with three straight losses.
© 2013 The Associated Press. All rights reserved. This material may not be published, broadcast, rewritten or redistributed.