Adeiny Hechavarria had two hits and scored two runs as Miami won for the fourth time in six games.
The Cardinals had a 3-1 lead before the Marlins scored four times in the fifth. The big blow was a bases-loaded triple for Dietrich, who was batting just .200 in August with one homer and three RBIs coming into the day.
Chris Narveson (1-0) pitched 2 2/3 innings for the win. He also drove in Hechavarria with a squeeze bunt in the fifth. Hechavarria was first called out, but the play was overturned after a 2 minute, 50 second replay review.
A.J. Ramos pitched ninth for his 19th save in 24 opportunities.
An error by St. Louis second baseman Kolten Wong and a single from Gordon set up Dietrich's big triple down the right-field line.
Miami starter David Phelps left in the third with right elbow stiffness.
Carlos Martinez (12-5) pitched five innings for St. Louis, allowing five runs, one earned, and seven hits.
Heyward connected in his first two at-bats against Phelps for his sixth career multihomer game and his first since Aug. 17, 2013 for Atlanta at Washington. Heyward's last four homers have come against Miami.
The Cardinals, who lead Pittsburgh by five games in the NL Central, won their first five games between the teams this season and also won the last final regular-season contest against Miami last year.
Marlins: RHP Jarred Cosart will pitch in a simulated game on Monday. If all goes well, he will head out on a rehabilitation assignment. Cosart has been slowed by vertigo.
Marlins: LHP Justin Nicolino (1-1, 4.86 ERA) will face Brewers RHP Matt Garza (6-12, 4.82 ERA) in the opener of a three-game series in Milwaukee on Monday. Nicolino is making his fourth major league start.
Link to box score: Miami (47-70) 6 at St. Louis (75-42) 4