Sandoval, last year's World Series MVP, had never hit three home runs in a game prior to Wednesday and had produced just 10 of them this season. He had four of San Francisco's 17 hits.
San Diego jumped to an early 2-0 lead, but Brandon Crawford hit a solo homer in the third to bring the Giants within one and San Francisco took the lead with a four-run fourth. That inning featured two-run homers from Hunter Pence and Hector Sanchez.
Sandoval then went to work, hitting a two-run blast in the sixth, adding a solo shot in the eighth and clubbing a three-run homer during a four-run ninth.
Lincecum (9-13) allowed seven hits and walked four in 5 2/3 innings. Five relievers combined to blank the Padres on two hits the rest of the way.
Eric Stults (8-13) suffered the loss, allowing seven runs on nine hits in 4 2/3.
In one of their highlights during a disappointing season, the Giants had their second largest run production of the campaign. They scored 14 runs in a win over Miami on Aug. 16.
NBC reportedly holds celebs hostage to Jimmy Fallon's show
Ray Liotta sues skin care company over use of likeness