Haywood Gilliam, a US judge in Oakland, California, made the ruling on Friday, saying the Pentagon money was not specifically authorized by Congress for construction of the barrier.
"The position that when Congress declines the Executive's request to appropriate funds, the Executive nonetheless may simply find a way to spend those funds 'without Congress' does not square with the fundamental separation of powers principles dating back to the earliest days of our Republic," the judge wrote in the order, Presstv Reported.
In all, the Pentagon is expected to shift about $6 billion to help build a border wall, including about $3.6 billion from military construction projects, some of which will be delayed.
The ruling compounds Trump's frustrations with federal court orders blocking his initiatives for curbing illegal immigration, a policy area he will focus on in his 2020 re-election campaign.
Trump has made toughening immigration policies a central tenet of his presidency and has vowed to build a wall along the US-Mexico border to combat illegal immigration and drug trafficking.
In February, after a protracted political battle and a government shutdown, Congress approved $1.38 billion for construction of “primary pedestrian fencing” along the border in southeastern Texas, well short of Trump’s demands.
To obtain the additional money, Trump declared a national emergency in February and his administration said it planned to divert $601 million from a US Treasury Department forfeiture fund, $2.5 billion earmarked for Department of Defense counter-narcotics programs and $3.6 billion from military construction projects.
The National Emergencies Act allows the president to utilize the armed forces and construction powers to build a border barrier.
Democratic lawmakers in Congress, more than a dozen states and two advocacy groups had asked Gilliam to block the transfer of military funds to prevent the wall construction.
They argue the Trump administration cannot use funds Congress has specifically denied and cannot construct a barrier that was not authorized, nor can the administration work outside the geographic area identified by Congress.
"This is a win for our system of checks and balances, the rule of law, and border communities," the American Civil Liberties Union tweeted.