ST. LOUIS -- Matt Carpenter knew his leadoff homer Tuesday night was important.
Choosing the latter, St. Louis peppered nemesis Jake Arrieta for five runs in the first two innings, then hung on by its fingernails at the end for an 8-7 win that kept Chicago from clinching its second straight National League Central title.
A wild three-hour, 46-minute battle that featured six homers, three triples and a whopping 14 walks wasn't decided until Juan Nicasio blew a 1-2 fastball by pinch-hitter Javier Baez with the tying and go-ahead runs on base.
It was the 31st pitch in Nicasio's four-out save, his third since joining the Cardinals three weeks ago and his fifth overall.
"We were hopeful when we got him that he could fill in on the back end," St. Louis manager Mike Matheny said. "After his first game, we decided to give him a shot, and he's looked very good. He's been a great asset for our club."
Nicasio also escaped a big jam in the eighth. Coming out of the bullpen after Ben Zobrist's two-out homer brought the Cubs (88-69) within a run, he gave up a ground-rule double to Kris Bryant on his first pitch but retired Anthony Rizzo on a popup for the third out.
"I can't say how important it was to get him," Carpenter said of Nicasio.
Long before Nicasio walked the tightrope in the last two innings, Carpenter and his teammates set the tone against Arrieta, who entered the night with a 2.06 career ERA in 16 career starts against the Cardinals (82-75).
Carpenter pounded a 420-foot homer on Arrieta's fifth pitch for just his second hit in 33 career at-bats against his former TCU teammate. Later in the first, Dexter Fowler tripled and scored when Paul DeJong's two-out grounder was booted by fill-in shortstop Mike Freeman. Randal Grichuk then rapped a triple to right center for a 3-0 lead.
An inning later, Carpenter worked a two-out walk. Tommy Pham ambushed a first-pitch fastball with nothing on it, drilling it 426 feet into the third deck in left to make it 5-1.
Arrieta (14-10) lasted only three innings and 67 pitches, allowing six hits and five runs with a walk and four strikeouts.
"Things just weren't working, sharpness-wise. It just wasn't his night," Chicago manager Joe Maddon said. "I didn't see anything overtly crisp, velocity or location."
St. Louis starter Carlos Martinez also had to work hard for his outs, making it through just 4 1/3 innings and 92 pitches before Matheny hooked him after Ian Happ flied out for the fifth inning's first out.
Martinez gave up four hits and three runs (two earned), walking four and whiffing six. Zach Duke (1-1) relieved and retired all five hitters he faced, fanning three, for his first win since June 20, 2016, when he was pitching for the Chicago White Sox.
Jedd Gyorko and Grichuk each clouted solo shots in the seventh to give the Cardinals what seemed like a safe 8-3 lead. But the Cubs rallied as Jason Heyward cracked a three-run shot on John Brebbia's first pitch in the eighth, followed by Zobrist's bomb.
However, Nicasio nursed the lead to the 27th out as St. Louis stayed 2 1/2 games behind Colorado for the NL's second wild-card spot. The Rockies blanked Miami 6-0.
Milwaukee's 7-6 win over Cincinnati locked Chicago's magic number at one for a second straight night. The Brewers, who finish the season with three games against the Cardinals in Busch Stadium, trail Colorado by 1 1/2 games.
NOTES: Chicago 2B Javier Baez (left knee) didn't start after leaving Monday night's game in the eighth inning for a pinch-runner. Baez was injured when he fouled a pitch off a knee. ... St. Louis All-Star C Yadier Molina wasn't in the lineup after taking consecutive foul balls off his mask in the seventh inning Monday night. He could return to action by Thursday night. ... Cubs OF Albert Almora left the game in the fifth inning after a collision with the left-center field wall dealt him a bruised shoulder.