Phillies 4, Giants 3 (12): Jimmy Rollins singled home the winning run in the bottom of the 12th inning and host Philadelphia avoided a three-game sweep.
John Mayberry Jr. swatted a pair of solo home runs for the Phillies, who snapped a seven-game home losing skid.
Kyle Kendrick (4-8) pitched 2 1/3 scoreless innings to hand San Francisco only its second loss in nine games since the All-Star break.
Carlos Ruiz ignited the winning rally by drawing a one-out walk off Brad Penny (0-1) before Laynce Nix followed with a single to put runners at the corners.
Rollins, who had only two hits in his previous 18 at-bats, ended it with a single to center to help Philadelphia boost its National League-worst home record to 18-29.
Nate Schierholtz clubbed a pair of solo home runs for the Giants, the second one tying the game at 3-3 in the top of the eighth inning.
Philadelphia starter Joe Blanton gave up three runs on seven hits in eight innings. Giants left-hander Barry Zito went seven innings, allowing three runs on five hits and recording a season-high seven strikeouts.
Schierholtz led off the game with his fourth homer before Chase Utley tied it in the bottom of the first with an RBI triple off Zito.
After Pablo Sandoval's RBI double in the fourth put the Giants ahead, Mayberry went deep in the fourth and seventh innings to give the Phillies a 3-2 lead before Schierholtz tied it in the eighth with his second solo blast.
GAME NOTEBOOK: Mayberry is 4-for-6 with three homers lifetime against Zito. It was his second career multi-homer game. ... Schierholtz's second homer was the 22nd surrendered by Blanton this season. ... Philadelphia activated OF Nix activated from the disabled list and designated OF Jason Pridie for assignment. ... Sandoval collected his first RBI on a hit since his three-run triple in the All-Star Game. ... The Giants return home to open a 10-game homestand with a three-game set against the San Diego Padres on Monday. ... Philadelphia continues its six-game homestand with a three-game series Monday against Milwaukee.