Ricky Weeks added a solo home run and Ryan Braun went 3 for 5 with a run scored for Milwaukee. Carlos Gomez went 2 for 4 and drove in two runs.
Ramirez, who also walked twice, reached base all five times he batted and scored twice. After a slow start, he has raised his average over the past 89 games from .218 to an even .300.
Mike Fiers (9-7) allowed one run and five hits in five innings and also got his first career RBI with a single in the second inning. He limited the Cardinals to five hits and three walks while striking out five. John Axford pitched a scoreless ninth for his 27th save.
Milwaukee improved to 15-4 in its last 19 games and pulled five games behind the Cardinals for the second wild card in the National League. St. Louis is one-half game up on the Dodgers for that spot with a four-game series against the Dodgers set for next weekend in Los Angeles.
The Cardinals - at 74-65 - have the same record they had after 139 games last season. St. Louis went on from there to capture its 11th World Series title.
The Brewers took a 2-0 lead in the second as Gomez's sacrifice drove in Ramirez, who had walked, and Fiers had his RBI single two batters later.
Daniel Descalso's two-out RBI single pulled St. Louis within a run in the bottom half of the inning.
However, Milwaukee regained its two-run lead in the third on a fielding error by Skip Shumaker on Corey Hart's grounder that scored Braun. Weeks' homer leading off the seventh off reliever Trevor Rosenthal made it 4-1.
The Cardinals pulled to 4-3 on Skip Schumacher's sacrifice fly in the eighth.
Ramirez went deep with one out the ninth against Fernando Salas to make it 5-3, and Gomez's RBI single closed the scoring.
NOTES: Joe Kelly (5-6) will face the Brewers' Shaun Marcum (5-4) in the series finale Sunday. ... The Cardinals' Matt Carpenter left the game in the third inning after cutting both hands when his bat shattered. The wounds were superficial and he is expected to be available Sunday. ... Weeks' home run gave Milwaukee an extra base hit for the 78th straight game. ... Carlos Beltran, who was hitless in the first five games of the homestand (0 for 9) was out of the lineup for St. Louis.
Link to boxscore: Milwaukee 6 at St. Louis 3