Buster Posey broke out of a slump with a first-inning home run as the San Francisco Giants beat the San Diego Padres 4-3 to salvage the final game of the three-game series.