"I still feel terrible because [Henderson] Alvarez battled and it would have been nice to get him the win," said McGehee, who attended high school in nearby Soquel. "At the same time, it was good for the team to get the win. We've seen these kinds of games go the other way, especially on the road."
McGehee came to bat twice with runners on base and he drove in runs each time. He's hitting .416 (32-for-77) with runners on this season.
"He had an opportunity to redeem himself and get a big hit," Marlins manager Mike Redmond said. "He's been doing that all year for us."
Christian Yelich, who led off the game with a home run and scored four times, drew a walk off Santiago Casilla to lead off the ninth.
"I'm just looking for a pitch," Yelich said. "I don't want to give away any outs. He missed with two curveballs and that made it a little easier."
Derek Dietrich, who hit a home run for the second straight night, followed with a single. One out later, McGehee delivered.
"Hitters got to eat, too," Giants reliever Jeremy Affeldt said. "It seemed like both offenses hit the ball well. They did some good situational hitting and sometimes you have to be OK with that."
Yelich led off the game with a home run, his first leadoff homer and the first by a Marlin this season.
"It was nice to get off to a good start like that," he said. "We had to battle for this one but the pitchers kept us in it."
For the second straight night, the Marlins took a 4-1 lead into the bottom of the fourth and failed to hang on as the Giants eventually tied the score in the sixth.
"It was the same kind of thing," Yelich said. "It was nice we stuck with it for a second night. It was good to get this one."
The Marlins loaded the bases with one out but Casilla induced pinch-hitter Ed Lucas to hit into an inning-ending double play.
The Marlins not only stayed out of the double play in the ninth, they added an insurance run on Garrett Jones' third hit of the night. Jones also made a nice catch of Hunter Pence's pop foul, reaching over the railing near the Marlins dugout to haul it in for the second out of the ninth.
"It seemed like this was a game where whoever made the big play would win," Yelich said.
Jarrod Saltamacchia broke out of a hitless streak in a big way, matching his career high with four hits.
"It was good to see him go out and have a big night," Redmond said. "He's had a rough go of it on this road trip."
Dietrich gave the Marlins a 3-1 lead with his two-run shot in the third. Adeiny Hechavarria drove in a run in the fourth.
Mike Dunn (4-3) pitched a scoreless eighth to pick up the win. Steve Cishek worked the ninth for his eighth save.
Starter Alvarez allowed five runs (four earned) on 10 hits over six innings. He did not walk a batter and struck out four. Since throwing his second shutout of the season against the New York Mets on May 6, Alvarez has given up 10 runs (nine earned) on 17 hits over 10 innings.
Saltamacchia ended a stretch of 26 at-bats without a hit when he singled against Giants starter Yusmeiro Petit with two outs in the second.
Giancarlo Stanton went 0-for-5, ending his 17-game hitting streak.