Locke (2-1) struck out six and walked two. After wriggling out of a jam in the fourth, he retired his last eight batters - striking out three of the final four.
Sanchez also doubled and Brandon Inge had an RBI double in his Pirates debut to help Pittsburgh win for the fifth time in seven games.
After opening the season with a pair of poor outings, Hamels (0-3) pitched well for the third straight start but was hurt by a lack of run support again. In eight innings, the left-hander gave up two runs on seven hits with six strikeouts and one walk.
Philadelphia, which has scored three runs or fewer in 13 of its 21 games, managed just four hits and had 11 strikeouts against Locke and three relievers.
Michael Young extended his hitting streak to 14 games with an eighth-inning single for the Phillies, who were shut out for the third time this season.
Jason Grilli pitched the ninth for his eighth save in eight chances. Grilli hasn't given up a run and has allowed just three hits with 15 strikeouts in nine innings this season.
Philadelphia appeared poised to score against Locke when John Mayberry Jr. led off the fourth with a triple and Locke hit Domonic Brown with a pitch to put runners on first and third with none out.
But Mayberry, running on contact, was tagged out trying to score on Ben Revere's weakly hit grounder to third and Erik Kratz hit into an inning-ending 5-4-3 double play to keep Philadelphia scoreless.
Locke didn't allow another batter to reach base before turning the game over to Pittsburgh's stingy bullpen, which entered third in the majors with a 1.94 ERA. The Pirates improved to 10-0 when leading after the seventh inning.
Pittsburgh got on the board in the fourth on Inge's RBI double to right that scored Neil Walker. Inge, who signed a free-agent contract with the Pirates in the offseason, was making his 2013 debut after being activated from the disabled list earlier Tuesday. The veteran infielder injured his shoulder in a spring training game on March 14.
Sanchez increased the Pirates' advantage to 2-0 with a one-out shot to right in the eighth.
Hamels carried a 10.97 ERA after his first two outings, but has pitched more like the three-time All-Star he is in his last three with a 2.57 ERA in those outings. Still, he has nothing to show for it as Philadelphia's slumping offense has done little for him - as it has for most of the Phillies' pitchers this season.
Philadelphia got a runner to second in the eighth against reliever Mark Melancon when pinch-hitter Laynce Nix singled and moved up on Young's two-out single. But Ryan Howard grounded out to end the threat.
Howard went 1 for 4 with two strikeouts and was one of several Phillies hitters who was booed by the crowd of 31,002 on a chilly evening.
NOTES: Pittsburgh C Russell Martin extended his hitting streak to eight games with an eighth-inning single. ... Left fielder Starling Marte's second-inning error on Revere's liner snapped an errorless streak of 41 1-3 innings for the Pirates. ... Phillies second baseman Chase Utley didn't start, but struck out while pinch-hitting in the eighth. He remained in the game in the ninth. ... Philadelphia RHP Roy Halladay (2-2, 6.04 ERA) is scheduled to face Pirates lefty Wandy Rodriguez (2-0, 0.56) on Wednesday night in the third game of the four-game series.