Gorkys Hernandez added a sacrifice fly as part of San Francisco's three-run 16th inning.
Ty Blach (4-5) earned the win by pitching 6 2/3 innings of scoreless relief. Former Marlins pitcher Sam Dyson got the last out -- with two runners on base -- to earn his first save of the season.
Giants third baseman Evan Longoria exited the game after being hit by pitch in the fourth inning. He was diagnosed with a fractured fifth metacarpal in his left hand -- the same injury that kept ace left-hander Madison Bumgarner from making his season debut until June 5.
Dereck Rodriguez -- son of Hall of Fame catcher Ivan "Pudge" Rodriguez -- was in line for his second major league win before the Marlins rallied against the Giants' bullpen in the ninth.
Rodriguez, a rookie right-hander who grew up in the Miami area and converted from outfielder to pitcher while in the minor leagues in 2013, lasted a career-best 6 2/3 innings. He allowed seven hits, no walks and two runs, striking out four.
The Marlins, who were looking for what would have been their first-ever four-game sweep over the Giants, had their win streak snapped at three.
Miami was led by backup catcher Bryan Holaday, who started the day batting just .161 but went 2-for-7 with one RBI. He also threw out a runner trying to steal against him for the ninth consecutive time, a franchise record.
Right fielder J.B. Shuck also had a big game for Miami, going 4-for-7 with two doubles.
Rodriguez was supported with some good early offense. For the second game in a row, San Francisco's Andrew McCutchen hit a two-run homer in the first inning. It was his eighth homer of the season and his fifth in the past 11 games.
Giants left fielder Mac Williamson also homered, his fourth of the season and his first since April 24. That solo shot came in the second inning.
Marlins right-hander Dan Straily, who last week escaped with only bruised (but not broken) ribs when hit by a 110 mph line drive off the bat of San Diego Padres slugger Eric Hosmer, didn't factor in the decision. Straily allowed three hits, one walk and three runs in five innings, striking out seven.
--Field Level Media