Left-handed reliever Felipe Rivero and minor league left-hander Taylor Hearn are headed to Pittsburgh in the deal.
Melancon, 31, is 1-1 with a 1.51 ERA and 30 saves this season. The right-hander likely will serve as the closer for the Nationals, who have squandered multiple late-game leads over the past week due to the struggles of veteran closer Jonathan Papelbon.
The Nationals announced that Melancon is expected to join them for Sunday's game against the San Francisco Giants. Washington agreed to the trade five days after the Chicago Cubs, another National League front-runner, bolstered their bullpen by acquiring hard-throwing closer Aroldis Chapman from the New York Yankees.
Melancon is fourth in the NL in saves this season and led the majors with 51 saves in 2015. The three-time All-Star had 130 saves and a 1.80 ERA over parts of four seasons with the Pirates.
Papelbon was pulled in the ninth inning of Thursday's game against the Giants and took the loss in two other games over the past week. The Nationals have used reliever Shawn Kelley to close out their last two games.