Alex Presley's double knocked in the game- 8 victory over the ending matchups at Miller Park.
Presley had three hits and three RBI as the Pirates won for the fourth time in six games.
Joel Hanrahan allowed the leadoff batter in the ninth to reach third on a single and two wild pitches, but worked out of the jam to earn his 40th save of the season.
Daniel McCutchen (5-3) stopped the bleeding in a three-run sixth inning to earn the win.
Yuniesky Betancourt knocked in four runs on two hits and a walk for the Brewers, who had won nine out of their previous 12 games entering the contest.
Takashi Saito (4-2) gave up Presley's double to take the loss.
With a loss by the Diamondbacks on Monday, Milwaukee still holds a one-game lead over Arizona for the second-best record in the National League.
"It was awesome to be able finally win a game here because they had something to play for," Presley said about the win for the Pirates, who had lost 12 straight games in Milwaukee. "It was nerve racking because the crowd was really into it."
Milwaukee failed to push across a run with the bases loaded in the eighth and then could not convert on an opportunity to tie the game in the ninth.
Craig Counsell started the ninth with a base hit up the middle and was then replaced by pinch-runner Carlos Gomez. Gomez was able to advance to third during a Corey Hart at-bat as Hanrahan threw two wild pitches. Hart eventually fanned on a slider for the first out.
Pinch-hitter Casey McGehee stepped up next and hit a grounder to second that Neil Walker fielded and threw home, easily beating Gomez to the plate and catcher Michael McKenry applied the tag. Pinch-hitter George Kottaras hit a weak grounder to second to end the game.
"We just kept coming back but couldn't push that run across," said Brewers third baseman Jerry Hairston Jr. "We battled back. That's all you can ask for."
Presley led off the game by sending a ball into the right-field bleachers to give the Pirates an early one-run advantage. It was Presley's first career leadoff homer.
Milwaukee answered with a run in the bottom of the frame when Nyjer Morgan singled and came around to score on Mark Kotsay's double to right.
The Pirates then scored three runs in the second to take back the lead. After Garrett Jones walked and Jason Jaramillo singled to right, Pedro Alvarez sent a base hit to center to score Jones. Jaramillo crossed the plate on a Chase d'Arnaud fielder's choice and d'Arnaud came home on Presley's single to right to make it a 4-1 game.
Betancourt and Hart hit run-scoring doubles in the fourth to make it a one- run game.
But Pittsburgh quickly padded the cushion in the fifth. After loading the bases with one out on an Xavier Paul triple and walks to Derrek Lee and Jones, Alvarez sent a double to left, scoring Paul and Lee. Jones then scored on a Frankie De La Cruz wild pitch to put the Pirates ahead 7-3.
Betancourt's two-run double in the home half of the fifth closed the gap.
Pittsburgh got a run back in the sixth on a Walker RBI single, but left the bases loaded in the inning when Jaramillo was called out on strikes.
Milwaukee tied the game in the bottom of the sixth, putting six straight batters on with two outs. Jared Hughes recorded two quick outs to start the frame, but then gave up a single to Morgan before walking Kotsay.
Tony Watson came on next and was greeted by a Prince Fielder base hit to right, scoring Morgan. Chris Leroux then took the bump and gave up a pinch-hit double to Ryan Braun to bring Kotsay across the plate. Consecutive walks to Hairston and Betancourt plated Fielder before McCutchen got Jonathan Lucroy to line out to short to end the inning.
Pittsburgh took the lead right back, though, in the seventh. After Saito struck out Alvarez and d'Arnaud to start the inning, McKenry walked and rounded the bases on Presley's double over the head of Morgan in center field.
Pirates center fielder Andrew McCutchen was scratched from the game after getting hit in the lower abdomen by a batted ball during batting practice...Charlie Morton allowed five runs on eight hits and three walks through a five-inning start for Pittsburgh...Shaun Marcum allowed seven runs on 10 hits and three walks in a 4 2/3 inning start for Milwaukee...The game lasted 4:13.