Matt Carpenter homered in the 10th inning against Craig Kimbrel in the All-Star closer's return from the injured list, and St. Louis regrouped after blowing a late lead to beat the Cubs 5-4 Thursday night. The teams will meet six more times in the final 10 days of the regular season.
Chicago dropped a game behind Milwaukee for the second NL wild card and four games behind St. Louis for first place in the NL Central.
Giovanny Gallegos pitched a perfect 10th for his first save in two chances. Andrew Miller (5-5) recorded the final two outs in the ninth for the victory.
Cubs slugger Anthony Rizzo provided a jolt in his unexpected return from a sprained right ankle, homering in the third inning to tie the game at 1. But Flaherty was spectacular otherwise, pitching three-hit ball while allowing just the one run and striking out eight. He left after 118 pitches with a 6-2 record with a 0.93 ERA in 10 starts since Aug. 1.
The Cubs finally broke through against closer Carlos Martinez in the ninth, which Nicholas Castellanos led off with a walk then advanced to second on Kris Bryant's single. After Kyle Schwarber was called out on strikes, Ben Zobrist hit an RBI double just inside the right field line. Willson Contreras followed with a slow roller on which Martinez threw late to the plate while Bryant scored from third.
Jason Heyward tied it with a groundout against Miller that scored pinch-runner Javier Baez from third.
Tommy Edman leadoff the fifth with a triple against Cubs starter Kyle Hendricks for the first of his two hits. Harrison Bader followed with an RBI single to snap a 1-1 tie.
The Cardinals took a 4-1 lead with a pair of runs in the sixth. Schwarber bobbled Yadier Molina's single in left field off Hendricks for a run-scoring error. Bader hit an RBI double off reliever Brad Wieck one out later.
Michael Wacha (6-7, 4.76) and Chicago RHP Alec Mills (1-0, 3.42) are the scheduled starters Friday. LHP Jose Quintana set to start for Chicago, but his assignment was pushed back to Saturday, when LHP Cole Hamels (fatigued left shoulder) had been scheduled to pitch. Hamels was scratched but hopes to rejoin the rotation next week.