Ryan Braun led off the inning by singling to right before Melancon (1-2) walked the next two hitters to load the bases with nobody out.
Up came Davis, who smashed his broken-bat hit into short center to drive in Braun and Jonathan Lucroy. Teammates showered Davis with cups of water in the face in celebration after he reached first.
Reliever Rob Wooten (1-1) threw 1-1-3 scoreless innings.
The comeback spoiled the return of Pirates starter Wandy Rodriguez from the disabled list with right knee inflammation. Rodriguez allowed solo homers to Rickie Weeks and Martin Maldonado but struck out four in his first start since April 19.
Gaby Sanchez's solo homer off Yovani Gallardo in the sixth snapped a 2-2 tie to give the Pirates the lead until Melancon broke down in the ninth. Subbing for injured closer Jason Grilli, Melancon had converted his four previous save opportunities.
Tony Sanchez added a two-run homer in the fifth, his first of the year. A reliever held up two arms in celebration as Sanchez hit the hanging 0-2 slider from Gallardo into the back of the Pirates bullpen beyond right.
Left-handers Justin Wilson and Tony Watson combined for six strikeouts in three scoreless innings of relief before the Brewers broke through late.
It was nearly good enough to get a win for Rodriguez, who fared much better Thursday than during two rehab starts in the minors. The veteran left-handed posted a 10.38 ERA with Double-A Altoona.
Rodriguez gave Pirates manager Clint Hurdle a few worrisome moments, like when Rickie Weeks and Jean Segura led off the bottom of the first with two hits. But Rodriguez snared a hard bouncer up the middle from Braun to start a 1-6-3 double play to help him get out of jam.
Weeks homered the other way into the right-field corner off a 2-1 slider in the third to extend the second baseman's hitting streak to eight. He finished 3 for 4.
The short-handed Brewers went a second day without star outfielder Carlos Gomez, who is nursing a sore back while serving a three-game suspension for his role in a benches-clearing brawl between the teams April 20 in Pittsburgh. The Brewers are also without usual cleanup hitter Aramis Ramirez, on the disabled list with a pulled hamstring.
Gallardo allowed five hits, three runs and struck out six in 6 1-3 innings.
Notes: The Brewers announced that English Premier League team Swansea and Mexican club Chivas would meet July 16 for the first professional soccer match at Miller Park. ... Manager Ron Roenicke said Gomez was feeling much better Thursday. Gomez left Tuesday's game with the injury, and his dropped his appeal of the suspension the next day. Gomez is eligible to return Saturday against the Cubs. ... Milwaukee opens the three-game set Friday in Chicago with starter Kyle Lohse (4-1). The Pirates start Edinson Volquez (1-3) to open a three-game Friday in New York against the Yankees.