With St. Louis trailing 4-3, Goldschmidt crushed a 2-2 pitch from Pirates reliever Michael Feliz into the bullpen in left-center field. Goldschmidt's 18th home run of the year provided the deciding runs.
The home run was Goldschmidt's only hit in three at-bats. He also drew a walk.
"Guys showed up and answered the bell," Cardinals manager Mike Shildt said. "Again, we expect it, but we don't take it for granted when they show up and you get down, and it was really about digging in and keep fighting."
Goldschmidt is hitting only .251 with 18 home runs and 43 RBIs in his first season with the Cardinals. The 31-year-old hit .299 with 201 home runs and 684 RBIs as the Arizona Diamondbacks' starting first baseman from 2012 to 2018.
Cardinals closer Carlos Martinez worked around Bryan Reynolds' RBI single in the ninth inning to preserve the win. Martinez is 2-1 with a 3.18 ERA and six saves in 20 games this year.
The Cardinals trail the first-place Chicago Cubs by three games in the NL Central Division after Wednesday's victory.