Javier Lopez (1-0) pitched the 10th for the win after Sergio Romo blew his second save in 14 opportunities, failing to finish off Barry Zito's gem.
Buster Posey hit a pinch-hit single to start the 10th against Antonio Bastardo (1-1) and was sacrificed to second by Joaquin Arias. Posey advanced to third on a wild pitch before Torres came through with his fourth career game-ending hit.
The Giants earned their fifth walkoff victory.
Zito pitched another gem in his home ballpark and Hunter Pence homered against his former club for the second time during the three-game series, but Romo wasn't his usual spot-on self in the ninth.
Pence sent a 2-2 pitch into the stands in left field leading off the second for his seventh homer. Marco Scutaro hit a go-ahead single in the fifth to score Gregor Blanco after he singled and stole second.
Still, Zito is 3-0 with a 0.55 ERA in his four home starts this season - and the home ERA ranked third-lowest in the majors. And San Francisco has won Zito's past 11 regular-season starts in the waterfront ballpark.
He pitched into the eighth, struck out three and didn't walk a batter for the first time this season.
Zito left to a rousing standing ovation from the sellout crowd of 41,048 in the eighth and added an RBI single in the sixth.
Coming off a pair of no-decisions against the division rival Padres and Dodgers, Zito helped get the Giants back on track following a pair of 6-2 losses on the heels of their season-best six-game winning streak.
Santiago Casilla took over from Zito after Carlos Ruiz's leadoff single, but he came out with a right knee injury two batters later and Jeremy Affeldt finished the eighth. Romo allowed Jimmy Rollins' leadoff double, an RBI single to Chase Utley and Delmon Young's tying sacrifice fly in the top of the ninth.
Kevin Frandsen hit his first home run of the year against his former team to tie the game leading off the fourth. He was booed as he rounded the bases. That was just the second earned run allowed by Zito in 33 innings at home this season.
Philadelphia did little else until late, and couldn't pull off the club's first three-game series sweep in San Francisco since May 18-20, 1984. The Phillies have never swept at 14-year-old AT&T Park.
Their 69-year-old skipper, Charlie Manuel, managed his 1,331st game for the franchise, matching Gene Mauch for the most in team history.
The Phillies lost for the first time in four career starts by Jonathan Pettibone, who threw 112 pitches over 5 2-3 innings.
Rollins doubled with one out in the sixth, but was thrown out at third for his second caught stealing in four attempts.
Zito's single in the sixth chased Philadelphia's rookie right-hander. With two outs, Pettibone intentionally walked Guillermo Quiroz to get to Zito, who singled to right for his fourth hit of the season and second RBI.
The Phillies received the news right-hander Roy Halladay would have arthroscopic shoulder surgery but could still return this year if all goes well. The team travels to the Arizona desert to continue a seven-game road trip that has shown some positive signs after Philadelphia dropped the final two games of its homestand - including a 14-2 defeat to the Marlins on Sunday.
An MRI exam on Giants center fielder Angel Pagan's right leg revealed a strained groin rather than a hamstring injury, which caused him to miss games Sunday and Monday before he returned Tuesday. He was held out of the lineup Wednesday.
"Angel is feeling better," manager Bruce Bochy said. "He can play but I want to be cautious with him. The last thing we need is a setback. We feel like this will go away. He wants to play, doctors say he can play if he can stand it, but it was my call to give him another day. He feels it hitting, not throwing or running."
Notes: Posey had the day off after a night game until his pinch-hit appearance in the 10th. ... The Phillies will call up RHP Tyler Cloyd from Triple-A Lehigh Valley to take Halladay's turn in the rotation Friday at Arizona.