A failed asylum seeker convicted of raping a 13-year-old girl is to be awarded damages after a judge ruled he was unlawfully detained in prison.
Electronic tagging should have been considered, the judge said
The 31-year-old, known as "A" for legal reasons, was held for 20 months after serving his sentence while authorities tried to deport him to Somalia.
A High Court judge ruled his detention became unlawful because of its length, and he was entitled to compensation.
Human rights lawyers said he could get up to £50,000 from the Home Office.
Mr Justice Calvert Smith, sitting in London, said the detention became unlawful because of its length, the impossibility of achieving removal and misleading statements for immigration officials.
He also said there had been a failure to consider releasing the man with an electronic tag, an option available since July 2005.
The judge said although "A" had been desperate to remain in the UK, that was not enough to displace the illegality of his detention.
He added that it was not necessary for the man to stay in the UK while damages were assessed.
But it is thought "A" may seek a further judicial review against attempts to deport him.
Media requests that the man should be identified given his criminal record were rejected.
The judge said naming him could hamper Home Office attempts to deport him.
"A" first arrived in the UK in May 1995.
He completed an eight-year prison sentence more than three years ago, for the rape of a young girl at knifepoint.