"It was just kind of something unexpected," Hamels said.
Shane Victorino hit a solo homer, his eighth, and added an RBI double and a single as the NL East leaders improved to a majors-best 19-5 in July.
The Phillies had to survive a shaky ninth inning by Brad Lidge, who picked up his 20th save in 26 opportunities.
Lidge gave up a two-run homer to Mark Reynolds, who hit a 2-1 delivery into an upper-deck restaurant in left field to slice Philadelphia's lead to 4-3. Lidge retired the next three men to nail down the victory.
But on this night the story was Hamels. He looked as sharp as he did last autumn, when he was named MVP of the World Series and the NL Championship Series.
Hamels gave up four hits, walked none and struck out nine. He won back-to-back starts for the first time since June 4.
"Hamels is getting there," Philadelphia manager Charlie Manuel said. "He's real close."
Asked what he's looking for from Hamels, Manuel smiled and said, "I'm looking for him to throw shutouts and no-hitters."
Hamels outpitched Arizona ace Dan Haren (10-6), who entered with an NL-low 2.14 ERA. Haren lost for only the second time since May 12.
"Going up against Haren, you know he's not going to allow too many runs," Hamels said.
After Roberts' shot into the pool area in right-center field gave Arizona a 1-0 lead, Hamels found his groove and began mowing down the Diamondbacks.
Hamels allowed back-to-back singles in the third, then retired 17 of 19 batters -- and one reached on an error.
"From the very first hitter, I knew I was able to locate," Hamels said. "I knew I was definitely going to be able to feel it today and just go out there and try to pitch my own game."
Hamels (7-5) also doubled in the fifth and came around to score Philadelphia's second run.
Facing one of baseball's most potent lineups, Haren went five innings, allowing three runs, two earned, and four hits. He walked three and struck out five.
"I don't know what was harder -- that [Phillies lineup] or the All-Star team lineup," Haren said.
Haren was perhaps the most dominant pitcher in the NL in the first half of the season. But he has typically slumped in the second half, and he's off to a slow start since appearing in his third All-Star Game.
Haren has a career post-All Star break ERA of 4.10 -- more than a run higher than his 3.08 ERA in the first half of the season.
Haren has lasted only five innings in each of his first two starts since the break -- his two shortest outings.
Fouling off pitch after pitch, the Phillies made Haren work.
"They put together a pretty good game plan against Dan," Arizona manager A.J. Hinch said. "They were going to wait him out."
The Phils tied it at 1-1 in the third when Victorino hit a 1-2 pitch from Haren deep into the right field seats.
Philadelphia made it 3-1 in the fifth, taking advantage of another Arizona meltdown in the field.
Arizona has 85 errors, second in the majors.
"I've said this time and again -- if we don't execute, we're going to have these peaks and valleys," Hinch said.
Reynolds has hit in 12 straight games. ... Philadelphia has won the first two games of the series despite going 3-for-22 with runners in scoring position. ... Phillies LHP J.A. Happ, who has been the subject of trade rumors, will start the series finale on Wednesday night. He's 7-1 with a 2.97 ERA. ... Arizona activated C Chris Snyder, who has been out since June 23 with a lower back strain. Snyder was the Diamondbacks' opening day catcher, but he'll back up Miguel Montero for now. ... Phils 2B Chase Utley was hit by a pitch for the 15th time, most in the NL.