Cosart allowed three hits and one walk to win for the first time since July 7. The victory came in his second start for Miami after being acquired from Houston in a trade for prospects.
He's 1-1 with Miami and 10-7 overall this year.
Wainwright (14-7), making his first start against the Marlins since 2010, missed a chance to became the first 15-game winner in the majors. He went seven innings and allowed three runs, with two coming on a home run off the foul pole by Donovan Solano — only the sixth homer allowed by the Cardinals' ace.
The Cardinals lost for the fourth time in five games. They had totaled 29 hits in the past two games but managed only four against Cosart and three relievers.
With closer Steve Cishek given the night off, Mike Dunn came on with two on and two out in the ninth and earned his first save by retiring Matt Adams. The Marlins improved to 15-10 since the All-Star break.
Miami's Giancarlo Stanton, who had hit five homers in his past five games, walked twice and grounded into two double plays.
Casey McGehee led off the Marlins' fourth and hustled for a double when left fielder Matt Holliday was slow getting the ball back into the infield. Jarrod Saltalamacchia singled home the game's first run, and Solano pulled an inside pitch for his second home run.
It was stunner coming against Wainwright, who began the game with the best innings-to-homers ratio among all major league starters. The homer was Solano's first ever at Marlins Park in 133 games.
Cosart, who missed his last turn because of a sore lower back, decided not to risk aggravating the injury at the plate. He took six strikes in two at-bats.
Justin Masterson, who got only six outs in his most recent outing, starts for the Cardinals against Nathan Eovaldi in the final game of the series Wednesday. Masterson has an 11.25 ERA in two starts since joining the Cardinals, but manager Mike Matheny said he was encouraged by the way the right-hander threw in his most recent bullpen session.
Link to box score: Miami (59-60) 3, St. Louis (62-56) 0
Join the pre-game, in-game and post-game discussion about each contest at The Cardinal Nation message board.