Jeff Mathis doubled in the deciding run in the bottom of the eighth, as Miami got past San Francisco, 6-5, in the finale of a three-game set.
Mathis added a solo homer, while Ed Lucas, Adeiny Hechavarria and Donovan Solano each drove in a run for the Marlins, who avoided a sweep and won for the third time in their last five.
"We've been struggling to score runs all year and we score runs the last two days and we can't come out on top," Mathis said. "Today it was back and forth again and it was just nice to get that one."
Chad Qualls (4-1) tossed 1 1/3 innings of scoreless relief and Steve Cishek followed suit in the ninth to earn his 27th save.
Hunter Pence slugged a two-run homer and Brandon Crawford added a solo blast for the Giants, whose three-game win streak ended.
Sandy Rosario (3-1) was charged with the winning run on three hits in his one inning on the mound.
Miami took a 6-5 lead in their last turn at the plate, as Ed Lucas started with a base hit and was able to cross home before Mathis was tagged out at third trying to stretch his RBI double.
Cishek yielded a long single with one out to Hector Sanchez, but he was erased on a game-ending double-play grounder from Buster Posey.
Pence's two-run shot to dead center gave the Giants the lead in the opening frame.
Mathis kicked off the home third with a shot to left which halved Miami's deficit, then RBI singles from Lucas and Hechavarria in the fourth put the Marlins ahead 3-2.
Pence's two-out single in the fifth plated Gregor Blanco to tie the game, but the hosts forged ahead in the home half when Giants starter Madison Bumgarner uncorked a wild pitch during a walk to Justin Ruggiano which allowed Solano to come in.
"It seemed like almost every pitch I threw was a hair off in the wrong direction," Bumgarner said. "I was just struggling making pitches today."
Crawford started the seventh with a homer, then after the next two batters went silently, Pablo Sandoval singled and Hector Sanchez roped a double down the left-field line which put San Francisco back on top, 5-4.
It was a 5-5 deadlock in the home half, as Christian Yelich worked a leadoff walk and scored when Solano followed with a double.
Marlins hurler Tom Koehler allowed seven hits and three runs with four walks and as many strikeouts over five-plus innings, while Bumgarner worked five full frames and surrendered four runs on five hits while walking four ...Coming into Sunday, Bumgarner has fashioned a 1.80 ERA in his past 11 starts since June 14, the third-lowest mark in the majors behind Clayton Kershaw and David Price ... Both teams are in action on Monday, as the Marlins host the Dodgers for four games and the Giants welcome the Red Sox for a three-game interleague set.