The Bush administration criticized the decision and said it will seek to block the order.
In a stern rebuke of the government, U.S. District Judge Ricardo M. Urbina said it would be wrong to continue holding the detainees since they are no longer considered enemy combatants. Known as Uighurs (pronounced WEE'-gurz), the men have been in custody for nearly seven years.
Over the objections of government lawyers who called the Uighurs a security risk, Urbina ordered their release in Washington D.C. by Friday. It was the first court-ordered release of Guantanamo detainees since the prison camp opened in 2002.
"Because the Constitution prohibits indefinite detentions without cause, the continued detention is unlawful," Urbina said, prompting cheers and applause from local Uighur residents and human rights activists who packed into the courtroom.
Justice Department spokesman Brian Roehrkasse said Tuesday that the government plans to ask a federal appeals court to step in while attorneys file an appeal. He said the detainees at the U.S. naval base in Cuba had admitted receiving weapons training in Afghanistan and were a national security risk.
Urbina, who was appointed to the bench by President Clinton, also ordered a hearing next week to decide where the Uighurs should be permanently settled. Until then, members of the Uighur community in the Washington area agreed to sponsor and help care for them.
"I think the moment has arrived for the court to shine the light of constitutionality on the reasons for the detention," he said.
Justice Department attorney John C. O'Quinn said the government would consider whether to appeal the decision. O'Quinn's request to delay the Uighurs' release pending a possible appeal was denied Tuesday by Urbina, who said the detainees had waited long enough.
Urbina said once the detainees arrived in Washington, they would be free to move around unsupervised, drawing the surprise of government attorneys who suggested that immigration officials might act to take the men into custody upon their arrival.
That prompted an angry response from the judge, who said he would not "take kindly" to such a government move. "That would be inappropriate," Urbina said. "There is a pressing need to have these people, who have been incarcerated for seven years, to have those conditions changed."
At issue is whether a federal judge has the authority to order the release of Guantanamo prisoners, who were unlawfully detained by the U.S. and cannot be sent back to their homeland. The Uighurs, who are Turkic-speaking Muslims in western China, have been cleared for release from Guantanamo since 2004 and ordinarily would have been sent home.
A spokesman at the Chinese Embassy in Washington did not immediately respond to a request for comment about Urbina's order. The government in Beijing has demanded that all the Uighur detainees be repatriated to China.
The Bush administration says the Uighurs cannot be sent back to China because they could be tortured. The Bush administration says no other country is willing to accept them. Albania accepted five Uighur detainees in 2006 but has since balked on taking others, partly for fear of diplomatic repurcussions from China.
Uighurs are from Xinjiang - an isolated region that borders Afghanistan, Pakistan and six Central Asian nations - and say they have been repressed by the Chinese government. China has long said that insurgents are leading an Islamic separatist movement in Xinjiang. The Uighur detainees were captured in Pakistan and Afghanistan in 2001.
Urbina's decision has broader implications for the future of the Guantanamo prison, which the Bush administration has said it wants to shut down after "working with other countries to take people back under the right circumstances."
A federal judge is set later this month to hold hearings on other Guantanamo prisoners challenging their detention as so-called enemy combatants.
Roughly 20 percent of about 250 detainees who remain at the military prison fear torture or persecution if they return to their home countries, according to the New York-based Center for Constitutional Rights. Their concerns raise similar questions as to where they should go if other countries refuse to take them. The U.S. has long maintained they should stay at Guantanamo.
"How many times does the Bush administration need to be told that detainees are entitled to essential rights? All the remaining detainees in Guantanamo Bay must be either charged and tried or released immediately," said Larry Cox, executive director of Amnesty International USA.
The Bush administration has argued that a federal judge cannot order the release of a foreign-born detainee into the U.S., saying that would undercut immigration laws that dictate how foreigners are brought into the country.
Until a country accepts the Uighurs, they would stay in special Guantanamo housing that includes TVs, air-conditioning and recreational items such as soccer, table tennis and volleyball, government attorneys said.
O'Quinn also said federal judges should defer to the executive branch officials, who he said must consider delicate relations with China. "The court should be circumspect because of the potential for interference with foreign relations," O'Quinn said.
Sabin Willett, an attorney for the Uighurs, countered: "I've never heard anyone argue our relations with other nations are a basis for holding someone."
Rebia Kadeer, president of the World Uighur Congress, called the decision a victory for oppressed Uighurs in China.
"This is our destiny. This is our people's win. This concerns our freedom. China accuses us of being terrorists, but we are not," she said through a translator as other Uighurs in the courtroom cried for joy.
Emi MacLean, an attorney with the Center for Constitutional Rights, said she hoped the decision would encourage other foreign countries to take in Guantanamo detainees who have not been charged.
"Finally, we are beginning the process of taking responsibility for our mistakes and fixing them," she said. "Allowing these wrongfully detained men a fresh start would also provide the U.S. a fresh start - an opportunity to turn a page and finally take a position of leadership in closing Guantanamo."