The US military base at Guantanamo has housed prisoners since 2002
In the first ruling of its kind, a US court has overturned the designation of an inmate at the Guantanamo Bay prison camp as an "enemy combatant".
The appeal court judges said the Pentagon must either free or transfer Huzaifa Parhat or hold a fresh hearing.
Mr Parhat, a Muslim from China, was captured in Afghanistan in 2001 and then sent to the US Guantanamo base.
The ruling is the latest setback for the Bush administration over its policy on Guantanamo detainees.
Earlier this month, a US Supreme Court ruling gave foreign suspects the right to challenge their detention at Guantanamo in the civilian courts.
Some 270 men are being held at the Guantanamo base in Cuba on suspicion of terrorism or links to al-Qaeda.
More than 190 of them have filed challenges with the US appeals court in Washington seeking to have their classification as enemy combatants overturned.
The case of Huzaifa Parhat, an ethnic Uighur from Xinjiang province in China, was the first to be heard.
The US government argued he was a member of the Eastern Turkistan Islamic Movement, which it said had links to al-Qaeda.
But Mr Parhat's lawyers said that he considered the Chinese government as his enemy and there was no evidence that he ever fought against US interests.
Uighur activists are seeking autonomy from China, and there are sporadic outbreaks of violence in the province.
Military decision 'invalid'
The ruling, by a three-judge panel at the US Court of Appeals for the District of Columbia Circuit, was in response to a petition under the Detainee Treatment Act of 2005.
This allows detainees a limited review of their status as "enemy combatants".
"The court directed the government to release or transfer Parhat, or to expeditiously hold a new tribunal," a notice from the court said.
The military's decision that he was an enemy combatant was "invalid", the court ruled.
The court also said that Mr Parhat could petition a federal judge for his immediate release in the light of the Supreme Court's ruling on 12 June.
"It's a tremendous day," P Sabin Willett, a lawyer for Mr Parhat, was quoted as saying by the Los Angeles Times.
"But Huzaifa Parhat is now in his seventh year of imprisonment at Guantanamo Bay, and he doesn't even know about this ruling because he's sitting in solitary confinement and we can't tell him about it."
A Pentagon spokesman said it was reviewing the court's decision and considering the options.
Mr Parhat is one of 17 Uighurs still being held at Guantanamo, even though the US authorities acknowledge that they pose no threat.
Their case has become a diplomatic and legal headache for the US, which has tried to find a country willing to accept the Uighurs at the same time as defending its decision to hold them as enemy combatants.
In 2006, five Uighurs were released from Guantanamo and allowed to seek asylum in Albania after the US said they could not be returned to China as they would face persecution there.