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. Lou Montanez led off the bottom of the 10th inning with a homer for the Phillies' walk-off victory.
Up next: The Phillies travel to Lakeland, Fla., to play the Tigers at 1:05 p.m. ET on Friday. Roy Halladay pitches for the Phillies. Pence and Carlos Ruiz are the two Phillies regulars making the trip. Prospects Freddy Galvis and Tyson Gillies also are set to be in the lineup.
Todd Zolecki is a reporter for MLB.com. This story was not subject to the approval of Major League Baseball or its clubs.