Cole (16-8) gave up two hits, struck out seven and walked three as he bounced back from his worst start of the season. He threw a 99-mph fastball on his 94th pitch.
Second-place Pittsburgh cut the Cardinals' lead in the NL Central to 5 1/2 games and earned its first series victory at Busch Stadium since April 2013. The teams are 8-8 against each other with one series to play at Pittsburgh in the final week of the season.
John Lackey (11-9) gave up three runs and 10 hits in 7 1/3 innings but extended his streak of quality starts at home to 15.
Cole also drove in his first run of the season with a two-out single in the seventh off Lackey. Cole was 12 when he was at Angels Stadium rooting for Lackey, then a rookie with the Angels, during his Game 7 win in the 2002 World Series.
The Cardinals did not get a hit off Cole until there was one out in the fifth, when Jon Jay singled to center on a soft liner.
Lackey breezed through the first three innings on 29 pitches but, after walking and running the bases in the bottom of the third, was tagged for four hits and one run in the fourth.
Lackey exited after giving up a home run to Ramirez, the 38th of his career against the Cardinals, with one out in the eighth. Rodriguez added a two-run shot off Steve Cishek, and Marte hit a two-run drive off Mitch Harris in the ninth.
Cardinals: OF Randal Grichuk (elbow) was activated from the disabled list but initially will be limited to pinch running. Grichuk could be available to hit by the weekend but is not expected to be cleared to throw until next week.
Pirates: LHP Jeff Locke (7-9, 4.66 ERA) will start against the Reds for the fourth time this season when Pittsburgh opens a three-game series at Cincinnati. He went 1-1 with a 5.06 ERA in his first three starts against the Reds.
Link to box score: Pittsburgh (81-54) 7 at St. Louis (87-49) 1