The Pirates grabbed an early lead on Josh Bell's run-scoring single. Pittsburgh extended the lead to 4-0 in the third on consecutive two-out singles by Andrew McCutchen, Bell and David Freese.
The Brewers broke through in the third on Keon Broxton's leadoff home run. After Eric Sogard walked, former Pirate Neil Walker smacked a homer to cut the lead to 4-3.
Milwaukee pulled even in the sixth on Travis Shaw's leadoff homer, the third home run off Pittsburgh starter Gerrit Cole.
Cole, who entered the game having won his last seven starts on the road, gave up four runs and five hits in six innings. He struck out nine and walked three.
Pittsburgh regained the lead in the seventh on Josh Harrison's RBI single. John Jaso scored from second base, sliding in just ahead of the tag, a call that stood after a brief review.
The Brewers tied it again in the bottom of the inning when Broxton led off with his second homer of the game, a shot to left off reliever George Kontos.
Bell led off the eighth with a triple and scored on Adam Frazier's single off Milwaukee reliever Anthony Swarzak (1-0).
Brewers starter Jimmy Nelson, who allowed a career-high 10 runs in his previous outing, settled down after the shaky third inning, retiring 11 consecutive batters at one point. He gave up five runs, three earned, and eight hits in 6 1/3 innings. Nelson struck out seven and walked one.
Corey Knebel earned his 24th save in 29 chances.
Pirates: C Francisco Cervelli remained out of the lineup. He left Sunday's games with left hand and wrist discomfort. Chris Stewart started in his place.
Brewers: SS Orlando Arcia sat out for the second consecutive game with back spasms. ... C Stephen Vogt (left knee sprain) went 2 for 3 while playing first base for Triple-A Colorado Springs in a game at Iowa. Vogt is expected to make one more rehab start before likely rejoining Milwaukee for a weekend series in Colorado. ... OF Ryan Braun got a routine day off. ... Swarzak pitched in relief for the first time since Aug. 10 after being sidelined with a stiff neck.
Pirates: Jameson Taillon (7-5) gets the start Thursday as Pittsburgh begins a four-game series at home against St. Louis. Taillon is 0-1 with a 4.91 ERA in three career starts against the Cardinals.
Brewers: Matt Garza (6-6) takes the mound as Milwaukee opens a three-game series at Colorado on Friday. Brandon Woodruff, who had been scheduled to start on Friday, will instead face the Rockies on Saturday.