Pittsburgh won its fourth in a row, extended the Cardinals' losing streak to seven and extended its lead over St. Louis in the National League Central to 2 1/2 games.
The Pirates need 17 victories over their final 55 games to produce their first winning season in 21 years.
St. Louis took a 4-2 lead in the top of the fourth, but Clint Barmes doubled home a Pittsburgh run in the bottom half of the inning, Andrew McCutchen had a sacrifice fly in the fifth and Martin put the Pirates in front in the eighth.
Neil Walker singled leading off the eighth against Trevor Rosenthal (1-2) and moved to second on a deep fly ball to left by Pedro Alvarez that was the second out of the inning. Martin then singled to left on an 0-1 pitch.
Mark Melancon promptly retired the side in order in the ninth to earn his fifth save. Tony Watson (3-1) worked the seventh and eighth innings to claim the victory.