CHICAGO -- A day after enduring one of the more gut-wrenching losses of the season, the Cardinals flipped the script at Wrigley Field on Saturday.
A ninth-inning home run by Carlos Beltran and a two-out RBI double by Jon Jay in the 10th lifted the Cardinals to a 5-4 victory over the Cubs that not only boosted morale but also bolstered the team's playoff chances.
"That was," said manager Mike Matheny, "as good as they get."
Milwaukee, with its afternoon loss, now sits 2 1/2 games behind the Cardinals for the second National League Wild Card spot. The Dodgers fell three back with a loss later in the day.
"After losing yesterday's ballgame, we kind of left the ballpark, honestly, feeling a little bit down," Beltran said. "It was a good thing that we were able to come here today and win this one. Every game for us means something."
The Cubs opened this series by stunning the Cardinals with two ninth-inning runs and an 11th-inning tally. This time, the Cardinals provided the dramatic finish.
Having squandered several earlier scoring opportunities, the Cardinals were up against Chicago closer Carlos Marmol and a 4-3 deficit in the ninth.
With one out, Beltran sealed his first 30-homer season since 2007 with a blast that stayed just inside the right-field foul pole. While just his second homer since Aug. 10, it was additional evidence to suggest that perhaps the Cardinals' first-half star is finding his way back on track.
Beltran has hit safely in seven of his last eight games. The second of his two hits on Saturday was clutch enough to also earn him a smooch.
"Carlos, I kissed him right on the cheek. I'm not afraid to tell the whole world, either," said starter Adam Wainwright, who left his seven-inning start trailing. "He saved me. He saved the team today."
The home run was the first Marmol had allowed since the All-Star break.
"I made one mistake," Marmol said. "I paid for it. That's how the game goes. He made a good swing; I take my hat off."
The come-from-behind win wasn't completed until the 10th.
A pair of two-out walks -- the second of which completed a terrific eight-pitch at-bat by pinch-hitter Bryan Anderson -- turned over the lineup. Jay, 0-for-5 on the day entering the at-bat, came through with his second game-winning, extra-inning hit of the week. His RBI double off reliever Jaye Chapman hugged the right-field line and stayed fair.
Matt Carpenter scored from second without a play.
"Everyone knows what's at stake now," Jay said. "We want to get back to the playoffs. We got a taste of it last year, and we definitely think we have the team to do it. We just have to do it. It was a big hit."
Jason Motte, whose unavailability on Friday was a factor in the team's late unraveling, wasn't going to sit this one out. Motte closed his 39th game by striking out the side. He has now pitched in six of the team's last seven games and has earned a save in five of those.
"It was tough yesterday not putting them in there, but we're going to have to do that," Matheny said of Motte and Mitchell Boggs, who helped Trevor Rosenthal close out a scoreless ninth on Saturday. "They've been very consistent."
Early on, the game delivered a see-saw of emotions.
In the opening innings, it looked as if the Cardinals, who have played 11 one-run games this month, might get a chance to coast to a win. Wainwright was breezing through with little trouble. And offensive opportunities came with regularity.
Allen Craig singled and scored the game's first run off Cubs starter Travis Wood in the second. Matt Holliday and Yadier Molina, who later left the game in the ninth because of back spasms, each drove home a run in the third.
So much more, however, was left on the table.
The Cardinals loaded the bases with one out in the second; a strikeout of Wainwright and groundout by Jay ended that threat. With runners on the corners in the sixth, Wainwright and Jay again couldn't push anyone home.
"A lot of times what we [pitchers] do at the plate is the difference between a win and a loss," Wainwright said. "If I come through early on, especially, we have the starter on the ropes right there [and] that game probably doesn't go like that."
Five times the Cardinals ended innings with a runner on third. The team stranded 13.
"This whole season, since May, it just seems like every game has been like this, where we're sitting on the edge of our seat," said Matheny, whose 42nd birthday was made much more pleasant by the outcome. "It builds some character and some fight, there's no question. But it all comes down to somebody getting that big hit and us beating them to the punch before they get it."
Wainwright's only substantial hiccup came in the sixth, when the Cubs finished erasing a three-run deficit with a three-run inning. A leadoff homer by David DeJesus pulled the Cubs to within one. Welington Castillo and Dave Sappelt then each collected an RBI.
Eight batters came to the plate in the 25-pitch inning.
"I was very, very disappointed that I gave up those three runs that inning," Wainwright said. "It just really hurt me deep. If we would have lost that game, I don't think I would have slept today at all."
Rookies Sam Freeman and Rosenthal followed Wainwright with scoreless appearances to get the game into the hands of Boggs and Motte.
The Cardinals are now 6-11 in extra innings this year.
"Huge win," Molina said. "We really needed this win. I'm so happy for the team."
Jenifer Langosch is a reporter for MLB.com. Read her blog, By Gosh, It's Langosch, and follow her on Twitter @LangoschMLB. This story was not subject to the approval of Major League Baseball or its clubs.