The Pirates went into the night's action just one game out of the top spot in the National League Central and they kept pressure on division-leading St. Louis thanks to Locke's first victory in almost two months.
Locke (10-5) had not posted a win since July 19 against Cincinnati, but he limited the Cubs to a lone run with five strikeouts while throwing 100 pitches and Pittsburgh ultimately survived a shaky save from Mark Melancon.
Chicago's run came in the third inning on an RBI single by Starlin Castro, but the Pirates took the lead with two in the fourth.
Pittsburgh put runners on first and third in that frame with two away, having had a runner thrown out at home during the inning. An infield single by Marlon Byrd accounted for one run and a base hit by Pedro Alvarez delivered the other.
The Pirates added an insurance run in the eighth when Jordy Mercer doubled in Clint Barmes, but Pittsburgh left the bases loaded.
Melancon came on in the ninth and put runners on first and second with nobody out. A sacrifice bunt attempt by Donnie Murphy turned into a popup to the catcher and Melancon retired the next two batters for his 14th save.
Chris Rusin (2-4) gave up two runs on just four hits in seven innings to take the loss.