Hamels was in line for his 11th career complete game as he batted to lead off the ninth.
The Phillies' offense then batted around, scoring six runs, and Hamels was lifted for pinch-hitter John Mayberry when his spot came up the second time in the inning.
The Phillies snapped a seven-game losing streak against Washington dating back to last year and avoided the sweep in the three-game series.
Washington rookie Bryce Harper stole home in the first inning, just the latest in an already growing list of accomplishments for the 19-year-old. Of more concern to Washington will be the status of right fielder Jayson Werth, who left the game in the sixth inning after apparently injuring his left wrist while attempting a sliding catch.
Werth's glove got caught underneath him and bent his wrist backward trying to grab Placido Polanco's sinking liner. Werth stayed on the ground briefly before throwing the ball back to the infield. He walked off holding his left wrist.
Werth missed the entire 2006 season after an injury to his left wrist which placed his career in jeopardy. He broke the wrist in spring training 2005 with the Dodgers and played 102 games that season before undergoing surgery to repair a torn ligament in the wrist.
Hamels (4-1) cruised after Harper's run. He didn't allow more than one base runner in an inning after the first, and struck out eight against one walk.
The game was close until Philadelphia's six-run ninth inning. Juan Pierre hit a run-scoring single, Shane Victorino followed with an RBI triple and then Pence lined a ball over the left-field fence for his second homer of the night and sixth this season. Pinch-hitter Freddy Galvis also singled home two runs before Mayberry struck out to end the inning.
The Nationals also scored a pair of runs against Philadelphia reliever Brian Sanches in the bottom of the ninth.
Washington Nationals starter Jordan Zimmermann (1-3) pitched into the seventh inning, giving up three runs and seven hits and walking four batters as the NL East-leading Nationals ended a four-game winning streak.
Zimmermann had not given up more than one earned run in any of his first four starts this season, but has allowed six over his past two outings. He also had not walked more than two batters in 30 straight starts coming into Sunday's game.
Victorino walked to lead off the fourth inning and then Pence hit a deep homer to center to give Philadelphia a 2-1 lead. The Phillies added another run with three more hits in the inning, including Polanco's RBI single.
Harper came up with two outs in the first and Hamels plunked him in the small of the back. Harper quickly shrugged off the sting, going from first to third on Werth's single to left.
When Hamels made a pickoff throw to first, Harper broke for home and slid in under the tag.
It was Harper's first stolen base in his eight-game major league career, and the second successful steal of home by a National since the team moved to Washington from Montreal in 2005.
In the third inning, Zimmermann hit Hamels in the left leg with one out and a runner on first when Hamels squared to bunt. Home plate umpire Andy Fletcher warned both dugouts.
NOTES: Nationals 3B Ryan Zimmerman (shoulder) took batting practice on the field before the game as he prepares to come off the disabled list. "It felt good," he said. "If everything continues to feel like it felt today, I'll play Tuesday." ... The only other steal of home by a Washington National came on April 20, 2011, when Ian Desmond stole home against St. Louis as part of a double steal. ... Philadelphia manager Charlie Manuel said LHP Cliff Lee (strained left oblique muscle) will come off the disabled list and pitch Wednesday against the Mets. ... Pro Football Hall of Famer Darrell Green and Basketball Hall of Famer Adrian Dantley were among six people inducted into the D.C. Sports Hall of Fame in a pregame ceremony.