Michael Morse gave the Giants their 50th home run of the season.
SAN FRANCISCO (AP) - Matt Cain's winless drought is over, even though the Giants' three-time All-Star pitcher is still struggling with some of his mechanics.
After nearly seven weeks of frustration, Cain isn't about to complain.
Buster Posey hit a go-ahead double as part of a three-run fifth inning and San Francisco held on to beat the Miami Marlins 6-4 on Thursday night.
Cain (1-3) earned his first win of the season despite another shaky outing at AT&T Park. The former San Francisco ace gave up two home runs and allowed all the Marlins' scoring in the first three innings before settling down to pitch into the eighth.
"He didn't cave in," Giants manager Bruce Bochy said. "He gave up four runs in the first three innings and here he is in the eighth. It says a lot about him. Good for him. It's got to be a good feeling."
Cain hadn't won since Aug. 17, 2013 - a string of 11 consecutive starts.
It looked like the drought might continue after Cain was nearly knocked out of the game in the third inning, but he stuck around long enough to get the win in his second outing since slicing the index finger on his right hand.
After giving up two home runs to the first seven batters he faced, Cain settled down to allowed six hits over 7 2-3 uneven innings. He struck out seven and walked three.
"I made some bad pitches and those guys really capitalized on it," Cain said. "I was making good pitches other than that. I just needed to eliminate the big one that kept costing me."
Mike Morse homered for the second consecutive game and drove in three runs while Hunter Pence added three hits and scored twice for the Giants, who trailed by three runs before rallying to bail out Cain.
Garrett Jones hit his sixth home run for Miami.