Derek Dietrich and Christian Yelich homered, and Jarrod Saltalamacchia snapped an 0-for-26 skid by tying his career high with four singles to stop Miami's slide. The Marlins entered the game losers of six of their past seven games and with a majors-worst 4-16 record away from home.
Santiago Casilla (1-1) got out of a bases-loaded jam in the eighth but couldn't escape trouble in the ninth. The right-hander allowed two baserunners before McGehee's tiebreaking single to right field.
Garrett Jones added an RBI single off lefty Javier Lopez and made a leaping catch in the netting of the photographers' well in foul territory in the ninth. Mike Dunn (4-3) pitched a scoreless eighth, and Steve Cishek converted his eighth save.
Neither starter pitched well but did just enough to give his team a chance in the final innings.
Henderson Alvarez allowed five runs - four earned - and 10 hits in six innings for the Marlins. He struck out four and walked none.
After wasting a 4-1 lead in a loss Thursday night to San Francisco, Miami moved ahead early and rallied back late this time.
Yelich hit the first leadoff home run of his career and first by a Marlins player this season when he connected over the wall in center. Dietrich homered for the second time in two nights and the fifth time this season, hitting a two-run shot to right to give the Marlins a 3-1 lead in the third.
Jones doubled and scored on Adeiny Hechavarria's flyout in the fourth to extend Miami's lead to 4-1 before San Francisco started to find its pop.
The Giants had three RBI hits with two outs, including pinch-hitter Gregor Blanco's bloop single in the sixth that tied it at 5-all. Blanco's hit came after McGehee couldn't corral a grounder at third.
Marlins manager Mike Redmond successfully challenged a transfer rule call in the fifth. After a television review, umpires ruled Marlins right fielder Giancarlo Stanton caught Hunter Pence's fly in the web of his glove and dropped the ball trying to transfer it to his hand for a throw.
Buster Posey's flyout to right later scored Angel Pagan, who had advanced to third on Stanton's botched transfer. Stanton made up for the play with an outstretched catch of Michael Morse's fly near the wall in the eighth.
Pagan also made a spectacular over-the-head catch on Dietrich's deep fly for the first out of the seventh and held on after running into the padded wall in left-center. And Casilla got pinch-hitter Ed Lucas to ground into a double play with the bases loaded to end the eighth.
NOTES: It was the fifth time Saltalamacchia had four hits in a game. ... Stanton's career-high 17-game hitting streak ended. ... San Francisco hosted ''Metallica Night,'' which included members of the heavy metal band playing the national anthem, drummer Lars Ulrich throwing out the ceremonial first pitch and singer James Hetfield announcing San Francisco's starting lineup. ... RHP Tim Lincecum (3-2 4.78 ERA) starts for the Giants opposite Miami's RHP Tom Koehler (3-3, 2.57) on Saturday.