Britain's most senior judge has forced Home Secretary David Blunkett to release a Libyan man detained by police for 15 months under anti-terror laws.
David Blunkett has been ordered to pay costs
The 37-year-old man, known only as "M", was released from Belmarsh prison in south-east London on Thursday evening.
Lord Chief Justice Woolf upheld a previous ruling that the detention of the man, who allegedly had links to al-Qaeda, was unjustified.
The Home Office said his ruling showed the legal safeguards in place worked.
M, who was arrested in November 2002, was one of 16 people to be detained under the Anti-terrorism, Crime and Security Act.
The act allows foreign nationals who are suspected of terror links but who cannot be deported to be held without charge or trial.
Two of the 16 decided to leave the country under the terms of the act while the other 14 appealed against their imprisonment.
But of the 13 appeals heard , M's is the first to have been successful.
The 37-year-old is now eligible to stay in the UK.
Mr Blunkett was appealing against a decision last week by the Special Immigration Appeals Commission (Siac) that M's detention was not justified.
Lord Chief Justice Woolf, who led a panel of three judges at the Court of Appeal, said on Thursday that if Siac ruled a detention was unlawful, the detainee should be released.
Siac had said that M's detention was based on evidence which was "wholly unreliable and should not have been used".
Mr Blunkett had not established "reasonable suspicion" that M had links to al-Qaeda, it had argued.
Government lawyers argued at the Court of Appeal that Siac's decision was "irrational and unreasonable".
Lord Woolf said the court accepted there was evidence which raised suspicions.
But he ruled Siac's decision was not defective and there was no prospect of a full appeal succeeding.
Human rights groups have welcomed the decision.
The UK director of Amnesty International, Kate Allen, said the challenge to Siac's decision should never have been allowed in the first place.
It was "tantamount to appealing against an acquittal verdict", she said.
"The verdict highlights the very real dangers of legislation that allows the UK government to detain people without charge or trial on the basis of secret "evidence" that would not stand up in a court of law."
However, a Home Office spokesman said the ruling had to be kept in perspective.
"In 12 out of 13 cases the home secretary's decision to use the power of certification under the ATCSA was justified and upheld in a court of law,"
"We intentionally created a robust system to provide legal scrutiny of the certifications made by the home secretary.
"We gave the courts the power to overturn decisions, that's what has been done today - this shows that the system works."
Lord Woolf, in giving his judgement, said: "The need for society to protect itself against acts of terrorism is self-evident.
"It remains of the greatest importance that, in a society which upholds the rule of law, if a person is detained as M was detained, that individual should have access to an independent tribunal or court which can adjudicate upon the question of whether the detention is lawful or not."
David Blunkett has been ordered to pay the costs of the appeal.