A convicted sex offender has fought off long-running Government attempts to deport him in what a judge criticised as a "procedurally very messy case" marked by legal confusion.
Wilfred Mosira, a Zimbabwe national, was convicted in 2012 on two counts of sexual activity with a 13-year-old girl and jailed for a total of three years.
His offences, committed when he was 25, meant he faced deportation as a "foreign criminal" under the UK Borders Act 2007.
Mosira was told that the then home secretary, Theresa May, intended to deport him on his release from prison.
His appeal to a first-tier immigration tribunal against deportation was rejected, but an upper tribunal ruled in September 2015 that it had not been open to the home secretary to withdraw his refugee status in the way she did.
The tribunal also held that, since his offending, Mosira now presented only a low risk to the public and there was not sufficient public interest justifying his removal under the rules of the 1951 Refugee Convention.
Government lawyers asked three judges in the Court of Appeal - Lady Justice Black, Lord Justice Sales and Lord Justice Henderson - to overturn the upper tribunal decision.
But on Thursday Lord Justice Sales said in the lead judgment the tribunal had carried out a proper assessment of the case, taking into account the remorse shown by Mosira and the low risk he currently posed.
Dismissing the Home Office appeal, the judge said: "This is a case in which the legal analysis by the secretary of state became confused at an early stage and was never reviewed and rectified. It also became procedurally very messy."
Both other appeal judges agreed.
Mosira came to the UK aged 17 in 2004 under a Government "family reunion" policy to join his mother who was previously granted refugee status because she was unable to obtain treatment in Zimbabwe for a medical condition.
The immigration authorities disputed his claims that he could not be returned to Zimbabwe as he would be in grave danger due to his political views, and complex legal arguments followed over the validity of his refugee status.
The upper tribunal saw evidence from a probation officer, who said in a July 2015 report that Mosira had made progress on an intensive six-month sex offender programme, there had been no further incidents and he was in a healthy and supportive relationship with a partner.
The tribunal ruled that on the available evidence he had rebutted the presumption that he constituted a danger to the community and his removal to Zimbabwe could not be justified.