PHILADELPHIA (AP) -- Matt Cain and Cole Hamels matched each other with better performances at the plate than on the mound.
The two All-Star pitchers hit homers in the same inning, and Gregor Blanco drove in the tiebreaking run with a drag bunt single in the 10th to lead the San Francisco Giants to a 6-5 victory over the Philadelphia Phillies on Saturday.
This was hardly the pitching duel expected from two of the elite starters in the majors. Both allowed three homers and five runs. But they did their job with the bat.
''I figured he'd try to put one out,'' Cain said. ''The most frustrating thing you can do is give up a homer to a pitcher.''
It was the first time pitchers homered off each other in the same game since Atlanta's Kevin Millwood and Colorado's Denny Stark did it on May 18, 2002. The last time it happened in the same inning was May 14, 1990, when Montreal's Kevin Gross and Los Angeles' Fernando Valenzuela went deep in the third inning.
''It was nice,'' Hamels said. ''I enjoyed it more after giving up my first ever homer to a pitcher.''
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.
Hamels allowed 10 hits 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 hits in eight innings. The five runs were tied for the most he's given up this year.
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.
''I said, 'Don't worry, have a good at-bat and forget about the other one,'' Blanco said. ''I was glad to have another opportunity.''
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.
''I can't control it,'' Hamels said.
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.
''Both pitchers made mistakes,'' Giants manager Bruce Bochy said. ''They're as good as anybody in the league.''
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.
Rob Maaddi can be reached at https://www.twitter.com/RobMaaddi