The Giants inched closer to a division title by winning their third game in a row.
Matt Cain (14-5), who did not have a decision in any of his previous three starts, was removed from the game after throwing 106 pitches over five innings. He allowed a run on four hits, but walked five.
A parade of six relievers worked the final three innings, combining to give up a run on five hits.
Tyler Skaggs (1-2) suffered through San Francisco's big third inning to take the loss.
The Giants loaded the bases with one out against Skaggs and Buster Posey reached on a fielder's choice that drove home a run. Pence then hit his grand slam on a 2-2 pitch.
Swim Week Miami Beach 2014 [PHOTOS]