Hamels (8-8) allowed a leadoff double to Ryan Garko in the second inning and a single to pinch-hitter Rich Aurilia to start the ninth. He struck out nine and walked one in his fourth career shutout and second this season. The left-hander extended his scoreless innings streak to 19, tying a career best, and won for the first time since July 28 against Arizona.
"He pitched a heck of a game," Phillies manager Charlie Manuel said. "He used all his pitches. When he gets going, he keeps it for a while. That's what we're looking for. He looked like the old Cole."
Jonathan Sanchez (6-11) was the hard-luck loser for the Giants. He allowed one run and three hits, striking out eight in six innings.
• Knobler: Giants' Penny says he'd return to AL East
The NL East-leading Phillies have won 15 of 20. They entered with a 7½-game lead over Atlanta.
The Giants came in tied with Colorado in the wild-card race.
"We got good pitching. We just got beat," manager Bruce Bochy said.
For much of this season, Hamels hasn't pitched like the guy who was MVP of the NLCS and World Series last October. But he was outstanding in his previous start and dominated San Francisco.
Hamels tossed eight scoreless innings in Philadelphia's 4-1, extra-inning win at Pittsburgh last Wednesday.
He was sharp right from the start of this one, mixing a sharp fastball with his trademark changeup and an effective curve. Hamels retired 24 in a row after Garko's hit.
"I really tried not to stress myself out and go out and not try to be too good," Hamels said. "All I can do is throw strikes and let the team behind me take care of it."
After Aurilia's single, Hamels picked off pinch-runner Andres Torres. He then walked pinch-hitter Pablo Sandoval, but struck out pinch-hitter Bengie Molina and retired Edgar Renteria on a foul pop to end it.
The offensively challenged Giants didn't have their top hitter, Sandoval, and cleanup hitter, Molina, in the starting lineup. Sandoval is bothered by a right calf injury that sidelined him for three games in the middle of last week. Molina hasn't started the last seven games because of a tight right quadriceps.
"If you are able to locate and throw strikes, no matter who is in the batter's box, you are going to have success," Hamels said.
Sanchez didn't allow a hit until Shane Victorino led off Philadelphia's fourth with a single to left. Victorino stole second, advanced to third on Chase Utley's groundout and scored when Howard ripped a double down the right-field line.
"The pitch to Howard was a good pitch, but he found a hole," Sanchez said.
Hamels even picked up his first career stolen base in the fifth after failing to execute a sacrifice bunt. He reached on a fielder's choice and then swiped second base without a throw before Jimmy Rollins grounded out to end the inning.
It was the first stolen base by a Phillies pitcher since Curt Schilling had one in 1997.
Hamels gave first-base coach Davey Lopes credit for the steal.
"He said, 'Have you ever slid?"' Hamels said. "I said, 'Yeah,' so he said, 'Go.' I was going to blame it on him if I was out."
- The Giants purchased RHP Brad Penny's contract from Triple-A Fresno. Penny, signed on Monday, is expected to start Wednesday night.
- San Francisco called up pitcher Waldis Joaquin, INF Jesus Guzman and OF John Bowker. Aurilia and Torres were activated from the disabled list.
- The Phillies called up LHP Jack Taschner. They're expected to add more players in the next week.
- Utley stole his 20th straight base, dating to last season. He has 17 this year.
- Phillies RHP Brett Myers pitched a perfect ninth and earned a save in a rehab appearance at Triple-A Lehigh Valley. Myers is trying to return from hip surgery in June.