The High Court has ruled that a foreign national jailed for 12 months for robbery is entitled to damages because he was detained too long while attempts were made to deport him.
A judge ruled Aziz Lamari, an Algerian citizen, should have been released last month.
By then it was clear there was no reasonable likelihood of deportation taking place within a reasonable time, and Lamari was suffering from mental illness "driven" by his detention, said the judge.
The Home Secretary was also found guilty of contempt of court because her department failed to release Lamari after it eventually accepted he should no longer be in detention and gave an undertaking to release him into suitable accommodation.
The 22-year-old Algerian national now joins a growing number of foreign criminals awarded compensation after being held too long under immigration rules pending attempts to deport.
Deputy High Court judge Barry Cotter QC said Lamari was detained after his robbery sentence came to an end in December 2010 and his deportation had been ordered.
By the time the High Court began hearing his case in May he had been held for over 17 months.
The judge said it was not in dispute that he had attempted suicide or serious harm on at least four occasions since April 2011.
Home Office lawyers argued detention before removal was clearly reasonable because of his "remarkable history of absconding and serious offending" and the risk of him re-offending.
In the nine months between July 2009 and April 2010 he was reported to have absconded three times, said Judge Cotter.
He also fled the country, was returned and was then convicted twice for offences of exposure, and later of robbery at Wood Green Crown Court in north London.
The judge said Lamari first arrived in the UK in July 2009 and was arrested within days and applied for asylum.
He was moved to Liverpool but absconded and was arrested in Cambridgeshire on August 13 after being found in the back of a lorry with three other people.
He absconded again and was arrested in Rotterdam in Holland and returned to England to have his asylum claim dealt with in the UK.
Judge Cotter said he absconded a third time in September 2009, and failed to turn up for an asylum interview a month later.
Two years later, in September 2011, he was diagnosed as having a "suicidal ideation" after making a serious suicide attempt and receiving treatment at Hillingdon hospital, north-west London.
When examined by a consultant psychiatrist in April 2012, soon after further suicide attempts, he was further diagnosed as suffering from a mixed anxiety-depressive disorder "driven" by his indefinite detention.
The judge said immigration case law stated the Home Secretary could only hold a person pending deportation for a reasonable period.
If it became clear that deportation could not take place within a reasonable period the power of detention should not be used.
Problems had arisen over getting Lamari sent back to Algeria, and it had now become clear that the Home Secretary was "effectively powerless to progress matters".
The judge said that by early September 2011 Lamari had been diagnosed as mentally ill and "there was a clear risk that the claimant would quite quickly deteriorate" as detention was "the driving factor of the mental condition".
He was "a young man now broken by the experience of custody" who was desperate to avoid further detention.
The judge ruled detention could no longer be justified after May this year, after the latest medical reports had been received and it became clear there was no realistic prospect of him being removed within a reasonable timeframe.
He said Lamari should have been released from detention, subject to suitable arrangements, by May 23 at the latest and was entitled to damages for the subsequent period of unlawful detention.
The judge ordered that the amount of damages should now be assessed, if not agreed, by the Queen's bench division of the High Court.
A UK Border Agency spokesman said an urgent review of the Lamari case was taking place.
The spokesman said: "Aziz Lamari is a failed asylum seeker who had served custodial sentences for serious offences.
"He was held in immigration detention awaiting removal to Algeria and we accept that he was not released on the date set by the court, which resulted in today's judgment.
"We are reviewing how this happened urgently."