The Pirates are now 23 games over .500, and are two and a half games up on the Cardinals (62-44) in the National League Central.
With the game tied 4-4 and two out in the bottom of the eighth, Russell Martin shot a base hit to center field off Trevor Rosenthal to bring home the go-ahead run from second.
Mark Melancon pitched a perfect ninth inning to seal the Pirates fourth consecutive win over their division rivals.
St. Louis belted out 13 hits in the game, but stranded 11 base runners.
Matt Holliday continued to hit the ball well driving in Carlos Beltran in the first to anchor the Cardinals to a 1-0 lead. Holliday went 3-5 with two RBI. He's hit safely in five of six games since returning from the disabled list.
Adam Wainwright pitched seven innings striking out six, but gave up four earned runs on eight hits. Pittsburgh scored once in four of the first five innings off Wainwright to keep pace with the Cardinals.
St. Louis attempts to avoid the rare five-game sweep Thursday. Joe Kelly faces Charlie Morton at 6:05 pm at PNC Park.