A judge has attacked the Home Office over what he said was the "shocking" handling of the case of a Zimbabwean man detained pending deportation.
The High Court judge said the rule of law must be applied to all
Mr Justice Munby said the failed asylum seeker - a convicted sex offender known as SK - was entitled to damages because he had been unlawfully detained.
The case was in breach of the law as it had not been reviewed regularly as required, the High Court judge said.
The Home Office said it regretted procedures had not been followed.
Mr Justice Munby said the fact that SK was a convicted sex offender and failed asylum seeker did not mean he had no right to the protection of the law.
SK, who arrived in the UK in 2002, had only been lawfully held from Monday after the Home Office rushed through a new review of his detention after the last lawful review in October, the judge said.
Mr Justice Munby went on to attack Home Secretary Jacqui Smith, saying "the secretary of state's blatant failure to follow her own policy in relation to review".
"The picture which emerges from this melancholy analysis of the secretary of state's file is deeply disturbing, indeed profoundly shocking," he said.
Rule of law
He told the court the case had been shocking "even to those who still live in the shadow of the damning admission by a former secretary of state [ex-home secretary John Reid] that a great department of state is 'unfit for purpose'.
"They are scandalous for what they expose as the seeming inability of that department to comply not merely with the law but with the very rule of law itself."
Although SK would "evoke sympathy in few hearts", "everyone was protected by the rule of law", the judge said.
The court was told that SK, who has now been detained for more than 22 months, arrived in the UK as a visitor in October 2002.
He was later granted permission to remain as a student until April 2004 but was jailed for a year in January 2006 on two counts of common assault and one of sexual assault on a female.
He was registered as a sex offender and, after his sentence was complete, was detained pending deportation.
However, Mr Justice Munby upheld complaints by SK that his detention had not been reviewed as required by the 2001 Detention Centre Rules and the Operations Enforcement Manual.
He had been entitled to 22 monthly reviews but only got 10 and his first was not until after 10 months, the court heard.
The judge therefore ruled that SK had been deprived of the safeguards prescribed by law and the Home Secretary's policy to protect against arbitrary detention, a principle enshrined in Article 5 of the European Convention on Human Rights.
The Home Secretary has been granted permission to appeal against the judge's decision on damages.
SK is also able to appeal against the court's ruling that he is now lawfully detained.
The judge rejected an application for bail.
Responding to the case, a Home Office spokesman said that despite making improvements to procedures, "we regret that they were not followed at all times".
"We will investigate why and how this has occurred and take further steps to strengthen our procedures."
However, he added that the Home Office was "pleased" the court had upheld its policy of detaining foreign nationals pending deportation "when we believe there is a risk of harm to the public".