All-Star game MVP Melky Cabrera hit a tying homer off Hamels in the eighth and Buster Posey hit a two-run shot for the NL West-leading Giants, who are 7-1 since the All-Star break.
Ryan Howard hit a three-run homer and Chase Utley also went deep for the last-place Phillies, who've lost three in a row and seven straight at home.
The five-time defending NL East champions are 41-54 and moving closer to perhaps being a seller before the non-waiver trade deadline on July 31.
This was hardly the pitching duel expected from two of the elite starters in the majors. Both pitchers did a better job at the plate than on the mound.
Hamels allowed five runs and 10 hits, including three homers, in 7 2-3 innings on a career-high 128 pitches. The three-time All-Star lefty is seeking a contract perhaps bigger than the megadeal Cain signed before the season.
Cain gave up five runs and five hits, including three homers, in eight innings.
Sergio Romo (3-1) retired the two batters he faced in the ninth and Santiago Casilla finished for his 24th save in 30 tries.
Cabrera walked to start the 10th against Jonathan Papelbon (2-4) and went to third on Posey's looping single to right. After Pablo Sandoval struck out, Blanco put down a perfect bunt down the first-base line and beat it out without a throw.
Blanco had missed a sign on a squeeze in the eighth. This time, the runner on third waited for contact before breaking toward the plate.
Cain signed a $112.5 million, five-year extension in April. The deal guaranteed him at least $141 million over seven years, including this season's salary and a 2018 buyout.
The Phillies are hoping to re-sign Hamels before the 2008 World Series MVP becomes a free agent after this season. If a deal isn't reached before the trade deadline, Hamels could be gone. Of course, that doesn't mean the Phillies couldn't re-sign him in the offseason. Hamels already has said he would be open to returning to Philadelphia, even if he's traded.
Fearing they were seeing Hamels with the Phillies for the last time, fans gave him loud cheers before the game and a standing ovation when he exited. Hamels waved to the crowd on his way to the dugout.
Down 4-2, the Phillies rallied in the sixth. Jimmy Rollins walked to start the inning. Shane Victorino followed with a bloop single to right for his 1,000th career hit.
After Utley flied out to shallow left, Howard connected. He ripped a 1-2 pitch to the seats in right-center to give the Phillies a 5-4 lead. Howard, the 2006 NL MVP, has three homers in 11 games after missing the first 84 games recovering from surgery on his left Achilles tendon.
But the Giants tied it at 5 when Cabrera led off the eighth with his 10th homer. Hamels hadn't allowed three homers in a game since June 26, 2010, vs. Toronto. That was 67 starts ago.
Posey followed with a double and advanced to third on a grounder. He was caught trying to steal home when Blanco took a pitch instead of bunting.
Hamels gave up three homers in his previous five starts combined before giving up two in the third.
First, Cain hit a towering shot to left for his sixth career homer and first this season. With two outs, Posey hit an 0-2 pitch out to left to put the Giants up 3-1.
Hamels answered Cain in the bottom half by taking him deep to right for his first career homer, cutting the deficit to 3-2.
Posey's two-out RBI single in the fifth made it 4-2.
Utley ripped a 3-2 pitch to the second deck in right field to give the Phillies a 1-0 lead in the first.
NOTES: Cain hadn't allowed three homers since Oct. 1, 2010, against Los Angeles. ... Giants CF Angel Pagan was ejected by plate umpire Mike Everitt in the sixth after arguing a called third strike. ... Giants are an MLB-best 20-9 against lefty starters. ... Phillies are an NL-worst 17-29 at home. ... Utley has four homers in 23 career at-bats against Cain. ... The Phillies had their 266th straight sellout, including postseason. ... Barry Zito (8-6, 3.75) opposes Joe Blanton (8-8, 4.79) when the Giants try to complete the three-game sweep Sunday.