ST. LOUIS – After losing a ball in the sun to let in the go-ahead run in the seventh inning, Braves center fielder Jordan Schafer had a chance to make amends when he came to bat with two out and the bases loaded in the ninth.

Schafer came through, drawing an eight-pitch walk against closer Trevor Rosenthal to bring in the tying run before the Braves scored the winning run on a wild pitch to give them a wild, sweep-averting 6-5 victory Sunday at Busch Stadium.

“Thank God we were able to pull out a victory, because if we had lost on that (sun ball) it wouldn’t have been a good flight home,” Schafer said. “It was nice to be given a chance to kind of redeem myself a little bit. I was trying to go up there and compete.”

Ryan Doumit, whose two-out pinch-hit double kept the ninth inning alive, scored the winning run when reliever Carlos Martinez threw a wild pitch to the first batter he faced, Ramiro Pena.

Closer Craig Kimbrel gave up a single and threw a wild pitch to advance a runner to second in the bottom of the ninth before getting Jhonny Peralta on a pop-up and striking out Peter Bourjos to end the game and collected his 11th save.

“That’s a good team, a real good team over there,” Doumit said after the Braves completed a 2-4 trip to San Francisco and St. Louis by beating the Cardinals for just the second time in six games this season. “It’s nice to take one from these guys and go home with a little momentum, because it’s not going to get any easier. We’ve got the Brewers coming in (to Atlanta) for four, the Rockies are playing pretty good baseball. So it’s nice to pull one out today.”

Were it not for a passed ball, a ball lost in the sun, and a high throw on a potential double play, the Braves might have pitched a shutout. The Cardinals capitalized on all of the above, as they had in the first two games of the series.

But just when it looked like the Braves would be swept, they came back against a closer who pitched for the fourth consecutive game. They staged a mood-swinging rally, beginning with a leadoff hit from Freddie Freeman, who had already homered in the first inning, been hit by a pitch in the fourth, and had a two-run, game-tying single in the sixth.

“Happy flight,” Freeman said. “We grinded all day, Gavin grinded all day and so did the hitters. We never caved today finally, and we (pulled) one out in the ninth.”

Rosenthal retired the next two batters after Freeman before Doumit doubled, and Evan Gattis was walked intentionally after Rosenthal fell behind in the count.

Schafer got ahead in the count 3-1, swung at a borderline pitch, then fouled off two pitches before taking ball four on the eighth pitch. Rosenthal was replaced at that point, and Martinez let in the second run in the inning as the Braves came back to win for just the sixth time in 18 games and the second time in their past nine road games.

The Braves got home runs from Freeman and Justin Upton, but they had only three other hits in seven innings against Cardinals left-hander Jaime Garcia, who gave up four runs in his first start in 12 months since shoulder surgery.

After Freeman’s two-run single in the sixth made the score, 4-4, center fielder Schafer lost a two-out fly ball in the sun in the seventh inning, turning Jhonny Peralta’s would-be routine third out into an RBI double off reliever Alex Wood, the left-hander who’s had hard luck all season whether starting or relieving.

“That’s the most helpless feeling in the world,” Schafer said of losing the ball in the sun. “The ball goes up, it’s fine. It comes down and halfway down it’s right in the middle of the sun. Just a helpless feeling.”

Braves starter Gavin Floyd was charged with seven hits and four runs (one earned) in 5-1/3 innings, with three walks and four strikeouts. He also hit two batters, including Garcia in the fourth inning in what appeared to be obvious retaliation for Garcia hitting Freeman in the back with a pitch immediately after Upton’s leadoff homer in the top of the inning.

Kolten Wong, who hurt the Braves with his speed Saturday, did damage with his bat Sunday when he lashed a two-out, three-run double in the second inning for a 3-1 lead. All the runs were unearned after a passed ball charged to Gerald Laird that allowed Peter Bourjos to reach base on a two-out strikeout.

After Upton’s homer brought the Braves to within a run, the Cardinals pushed the lead to 4-2 on Matt Adams’ sacrifice fly in the fifth. That run might also have been prevented if the Braves had completed a double play after Matt Holliday’s leadoff single. Dan Uggla throw to first base was high and pulled Freeman off the base.