Hamels allowed four hits and struck out two.
The Bucs threatened against Hamels after two quick outs in the fourth, loading the bases on a Rod Barajas single, a Matt Hague single and an error by second baseman Ty Wigginton that allowed Nate McLouth to reach base. Dave Bush then came on in relief of Hamels and induced a groundout by Gorkys Hernandez to end the inning.
The Phillies took a 1-0 lead in the second, when Hunter Pence singled and Wigginton doubled. They made it 2-0 in the fourth, when Pirates pitcher Justin Wilson allowed a single to Jim Thome and walked Wigginton, John Mayberry Jr. and Laynce Nix, forcing in a run.
Shane Victorino blasted a two-run home run in the bottom of the fifth.
James McDonald got the start for the Pirates, allowing one run on two hits and striking out one in three innings.
The Bucs got on the board in the sixth against Bush, with Casey McGehee driving in Josh Harrison on a single, his first hit with the club this spring. Barajas added a double and was lifted for a pinch-runner before Hague grounded out to score McGehee. McLouth singled, and then the Pirates made it 4-3 on a Hernandez sac fly.
Starling Marte tied the game in the eighth on a homer, sending the game into extras.
"We're very happy with what he's done," manager Clint Hurdle said of Marte. "It's exciting to watch him play. I'm very happy to see him come in with the comfort zone he has at the plate and on the bases. I see what everybody has been talking about in our player development system and the raves out of winter ball by people in other organizations who've watched him play."
Lou Montanez led off the bottom of the 10th inning with a homer for the Phillies' walk-off victory.
Up next: Shairon Martis, the former Washington right-hander signed to a Minor League contract following his release by the Nationals, makes his first spring start as the Bucs bus to Ft. Myers, Fla., for a 7:05 p.m. ET meeting with the Red Sox, available live on MLB.TV. Martis will try to build on his two shutout innings on Sunday vs. Toronto. The regular right side of the infield -- third baseman Pedro Alvarez and shortstop Clint Barmes -- will return to action.
Todd Zolecki is a reporter for MLB.com. This story was not subject to the approval of Major League Baseball or its clubs.