The Pirates received left-handed reliever Felipe Rivero and minor league left-hander Taylor Hearn as part of the trade.
The 31-year-old, right-handed Melancon is 1-1 with a 1.51 ERA and 30 saves in 45 appearances this season. He is a three-time All-Star with a 2.65 career ERA and 151 saves in eight seasons with the New York Yankees, Houston Astros, Boston Red Sox and Pirates. He saved a league-high 51 games in 2015.
Melancon is expected to serve as the closer for the Nationals after veteran closer Jonathan Papelbon struggled to close out games in the ninth over the past week. Papelbon was pulled in the ninth inning of Thursday's game against the San Francisco Giants and took the loss in two other games over the past week. Papelbon, 35, is 2-4 with 19 saves this season and his ERA has ballooned to 4.41 in 35 appearances.
The Nationals made the trade five days after the Chicago Cubs bolstered their bullpen by acquiring hard-throwing closer Aroldis Chapman from the Yankees.
Rivero, 25, went 0-3 with one save and a 4.53 ERA in 47 relief appearances this season with the Nationals. He was 2-4 with a 3.67 ERA in 96 career games for Washington.
Hearn, 21, went a combined 1-0 with a 2.79 ERA in 10 games (four starts) between the Rookie-level Gulf Coast League Nationals and Single-A Hagerstown. He was a fifth-round selection in 2015 out of Oklahoma Baptist University.