Ryan Doumit's RBI single off closer Ryan Franklin (2-2) tied it at 5. Garrett Jones followed with a single to center field, which scored Andrew McCutchen from second base to win it.
It was the Pirates' 54th victory of the season and snapped a nine-game losing skid. More importantly, it staved off their 82nd loss and, for now, a 17th straight losing season.
The Pirates remain tied with the 1933-48 Philadelphia Phillies for the longest streak of sub-.500 records in North American professional sports history
The Pirates last had a winning season in 1992, when they went 96-66 and won the National League East Division. They lost the NL Championship Series in seven games against the Atlanta Braves.
Yesterday's game, like so many in these 17 seasons, had an ominous start.
Julio Lugo stroked Paul Maholm's first pitch into center field for a double. Brendan Ryan sacrificed Lugo to second.
After Albert Pujols took a ball to even the count at 1-1, catcher Jason Jaramillo lobbed the ball back over Maholm's head. Lugo scored on the error.
The Pirates tied the game in the bottom of the inning. Andrew McCutchen singled, moved up on a grounder and scored on Lasting Milledge's two-out single to center.
The Cardinals regained the lead in the fourth. Ryan Ludwick was hit by a pitch and scored on Yadier Molina's single.
In the fifth, St. Louis got two insurance runs. With one out, Ryan doubled. Even with first base open, Maholm pitched to Pujols, who lined an RBI single to center. The play at the plate was close, but Ryan eluded Jaramillo's tag as Pujols went to second.
Ludwick's two-out single scored Pujols.
The Pirates tied the game 4-4 by scoring three runs in the sixth inning.
With two outs and Garrett Jones at first base, Brandon Moss bounced a ground-rule double over the wall in center field. Neil Walker walked, loading the bases.
Ronny Cedeno blooped a two-run single to center. Jason Jaramillo, whose throwing error let a run score in the first inning, lined a single up the middle. Walker scored to tie the game.
Rick Ankiel hit a pinch-hit homer in the ninth off Jesse Chavez (1-4), who wound up with his first major league victory.Sign up for breaking news alerts from FOXSports.com!