After the clubs split games Monday and Tuesday in which there were four hit batsmen and two ejections, both teams were on their best behavior in the series finale, the seventh time the clubs have met in the last 10 days.
Down 1-0, the Giants took the lead for good in the sixth after a walk to Kelby Tomlinson, a single by Joe Panik and a ground-rule double by Brandon Belt tied the score.
One out later, after Pablo Sandoval had been intentionally walked to load the bases, the Marlins went for a potential inning-ending double play on a Mac Williamson grounder to third baseman Miguel Rojas. However, Williamson beat the relay to first base, allowing Tomlinson to score the go-ahead run and keep the inning alive.
Pence followed with an RBI single that increased the lead to 3-1, and after he stole second, Hernandez smacked his two-run hit that broke the game open and completed the five-run uprising.
Pence, who was making his first start since June 12, added what turned out to be a critical RBI single in the eighth inning after the Marlins had closed within 5-3 in the seventh.
Left-hander Derek Holland (5-7) got the win, allowing three runs and seven hits in six-plus innings. He walked two and struck out seven.
The Marlins sent Holland to the showers when Lewis Brinson doubled and Rojas singled to lead off the seventh, with Miami trailing 5-1.
Lefty Tony Watson allowed the inherited runners to score on a sacrifice fly by pinch hitter Yadiel Rivera and an RBI single by Starlin Castro, but he shut the door after the Marlins had put the potential tying runs on base.
Miami wasn't done. In the ninth, a sacrifice fly by Castro and a two-out RBI single by Brian Anderson got the Marlins within 6-5, knocking Sam Dyson from the game.
Reyes Moronta came on with two aboard and struck out J.B. Shuck for his first career save.
Right-hander Jose Urena (2-9) took the loss, allowing five runs and eight hits in 5 2/3 innings. He walked two and struck out five.
Belt had three hits, including a double, for the Giants, who held a lead at some point in all seven of their games against the Marlins over the past 10 days and yet won just three of them.
Pence and Nick Hundley had two hits each for San Francisco, which improved to 2-1 on its 10-game homestand.
Rojas and Anderson collected three hits apiece and Castro had two RBIs for the Marlins, who fell to 3-3 on their nine-game trip.
Cameron Maybin and Derek Dietrich had two hits apiece for the Marlins, who out-hit the Giants 13-10.
--Field Level Media