US District Judge Jia Cobb, an appointee of former President Joe Biden, temporarily blocked the Trump administration from deploying National Guard troops to enforce the law in the nation's capital without approval from its mayor.
However, she paused her ruling until December 11 to allow the administration to appeal.
The legal fight is playing out alongside several others across America as Trump presses against longstanding but rarely tested constraints on presidents using troops to enforce domestic law.
White House spokeswoman Abigail Jackson said in a statement that Trump acted lawfully and called the lawsuit an attempt to undermine his successful efforts to stop violent crime.
District of Columbia Attorney General Brian Schwalb said in a statement that permitting Trump to use troops to enforce domestic law would set a dangerous precedent.
Schwalb, an elected Democrat, sued on September 4 after Trump announced the deployment on August 11. The lawsuit accused Trump of unlawfully usurping control of the city's law enforcement and violating a law prohibiting troops from doing domestic police work.
Trump has unique law-enforcement powers in Washington, which is not part of any state, but local officials say he overstepped by supplanting the mayor's policing authority and violated legal prohibitions against federal troops doing civilian police work.
Trump administration lawyers called the lawsuit a political stunt in court filings and said the president is free to deploy troops to Washington without the approval of local leaders.
The administration also has said the troops are operating lawfully and successfully reducing crime.
Trump, a Republican, has also moved to deploy troops in Los Angeles, Chicago and Portland, Oregon, to combat what he describes as lawlessness and violent unrest over his crackdown on illegal immigration.
Democratic leaders of those cities and their states have sued to block the deployments, saying they amount to an attempt to punish political foes with militarised shows of force.
Trial courts have ruled against the troop deployments in every city where local leaders protested their presence, although an appeals court has blocked one of those rulings and allowed troops to remain in Los Angeles.