Milwaukee let a four-run lead get away, then overcame a three-run deficit to win its fifth straight and stay within 2 1/2 games of St. Louis for the second National League wild card berth.
Pittsburgh's third straight loss dropped the Pirates a game under .500 for the first time since May 29, when they were 24-25. Pittsburgh had a 62-46 record on Aug. 6 before falling into its late-season slump.
The Brewers trailed 7-4 heading into the eighth, but Norichika Aoki doubled home a run in that inning and then scored along with Jean Segura on a triple by Rickie Weeks.
Ramirez followed with a single to center that scored Weeks with the go-ahead run. Logan Schafer added a run-scoring base hit in the ninth.
Chris Resop (1-4), the sixth of seven Pittsburgh pitchers, was charged with the loss.
Milwaukee has won 19 of its last 24 games to jump into playoff contention.
Wisconsin business offering 'therapeutic cuddling' forced to close
Video of Victoria’s Secret models trying to 'twerk' hits Instagram