Imposed last month by the D.C. City Council, the prohibition, due to take effect next month, is being closely watched by other cities concerned with the threat of attacks on their transportation networks.
Some city governments are also alarmed by a series of recent derailments or other mishaps in which leaking chemicals released dangerous fumes in or near populated areas, including one case this past weekend in Utah and another in South Carolina in January in which nine people died.
The Justice Department filed a brief with the federal district court in Washington where freight rail giant CSX Corp. is seeking to block the city's 90-day ban approved last month that takes effect April 11. The city was not satisfied with government efforts to secure the rail line.
The administration said the CSX and homeland security officials have "undertaken a specific assessment" of security needs for Washington, and government regulations already cover the routing of hazardous materials by CSX.
"The court should enter an order declaring the D.C. Act invalid," the administration said. The council cited studies that an attack on a train or a truck carrying certain hazardous materials in or close to Washington could cause thousands of deaths and billions of dollars in economic damages.
The ban covers a fraction of shipped hazardous materials, like flammable gases and explosives. It would permit their shipment if the railroad could prove it would be too costly to change routes or if there was no alternative route.
The government's legal brief, called a statement of interest, covered Justice, Homeland Security and Transportation department opinions on the matter.
The administration said the restriction could compromise security and safety because rerouting trains around the nation's capital lengthens travel time, potentially increasing the vulnerability of a hazardous shipment.
Federal officials also said rerouting trains around certain communities unfairly shifts risks onto others and can clog rail lines and train yards with new traffic, which could disrupt delivery networks and harm the economy.
Moreover, the administration said in the district court filing and another one with the federal Surface Transportation Board that federal security regulations already in place and strengthened after 2001 attacks on New York and Washington supersede local ordinances.
The court has yet to rule in the case but may wait for the Surface Transportation Board to make its determination on a similar challenge by CSX.
"We certainly appreciate the supporting statements from these key federal agencies and believe that it will add even more credible support to our arguments," said Gary Sease, a CSX spokesman.
Legislation has been proposed in Congress to toughen federal oversight of hazardous materials shipments.
(Additional reporting by Reshma Kapadia in New York)