Shamima Begum should be allowed to return to the UK to fight the decision to remove her British citizenship, the Court of Appeal has ruled.
Ms Begum, now 20, was one of three schoolgirls who left London to join the Islamic State group in Syria in 2015.
Her citizenship was revoked by the Home Office on security grounds after she was found in a refugee camp in 2019.
The Court of Appeal said she had been denied a fair hearing because she could not make her case from the Syrian camp.
The Home Office said the decision was "very disappointing" and it would "apply for permission to appeal".
The ruling means the government must now find a way to allow the 20-year-old, who is currently in Camp Roj in northern Syria, to appear in court in London despite repeatedly saying it would not assist removing her from Syria.
Lord Justice Flaux - sitting with Lady Justice King and Lord Justice Singh - said: "Fairness and justice must, on the facts of this case, outweigh the national security concerns, so that the leave to enter appeals should be allowed."
The judge also said that the national security concerns about her "could be addressed and managed if she returns to the United Kingdom".
Former Home Secretary Sajid Javid, who made the decision to strip Ms Begum of her citizenship in February 2019, tweeted a statement saying he was "deeply concerned about the judgement".
He said that regardless of the outcome of her case, if Ms Begum came to the UK "it will prove impossible to subsequently remove her".
Begum 'not afraid of facing justice'
Daniel Furner, Ms Begum's solicitor, said: "Ms Begum has never had a fair opportunity to give her side of the story.
"She is not afraid of facing British justice, she welcomes it. But the stripping of her citizenship without a chance to clear her name is not justice, it is the opposite."
Her father Ahmed Ali told the BBC he was "delighted" by the ruling, adding that he hoped his daughter would get "justice".
The prime minister's official spokesman said that while the government "doesn't routinely comment on individual cases", the decisions it made about Ms Begum had not been "taken lightly".
He said the government would "always ensure the safety and security of the UK and will not allow anything to jeopardise this".
Shamima Begum is not yet packing her bags to return to the UK - there's no government plane warming up the engines at a military airfield to bring the young Eastender home.
But the Court of Appeal could not have been clearer in its wording - she needs to be allowed back to make her case in the interests of justice.
This is an unprecedented ruling - and the government has a matter of weeks to convince the Supreme Court to look at it again.
If it stands, it could have major implications for the UK's policy of excluding some British-born IS supporters by depriving them of nationality once they're out of the UK.
Scores of these people - all deemed a threat to national security - could seek to return to the UK as they fight their case to get back their British citizenship.
Other governments have voluntarily repatriated these fighters and sought to contain their threat through prosecutions, monitoring and intensive deradicalisation. The UK has so far refused to do the same.
Ms Begum's legal team challenged the move on three grounds - that it was unlawful because it left her stateless; it exposed her to a real risk of death or inhuman and degrading treatment; and she could not effectively challenge the decision while she was barred from returning to the UK.
Under international law, it is only legal to revoke someone's citizenship if an individual is entitled to citizenship of another country.
In February, a specialist tribunal - the Special Immigration Appeals Commission (SIAC) - ruled that the decision to remove Ms Begum's citizenship was lawful because she was "a citizen of Bangladesh by descent".
She is understood to have a claim to Bangladeshi nationality through her mother.
SIAC, a semi-secret court which hears national security cases, also said that although there were concerns about how Ms Begum could take part in the proceedings in London, those difficulties did not mean the home secretary's decision should be overturned.
In his ruling on Thursday, Lord Justice Flaux said: "It is difficult to conceive of any case where a court or tribunal has said we cannot hold a fair trial, but we are going to go on anyway."
Government 'shirked' responsibilities
Human rights organisation Liberty, which intervened in Ms Begum's appeal, welcomed the ruling, saying the right to a fair trial was "a fundamental part of our justice system and equal access to justice must apply to everyone".
Liberty lawyer Katie Lines added: "Banishing someone is the act of a government shirking its responsibilities and it is critical that cruel and irresponsible government decisions can be properly challenged and overturned."
Ms Begum left Bethnal Green, in east London, aged 15 for Syria in February 2015, with two school friends.
Within days she had crossed the Turkish border and eventually reached the IS headquarters at Raqqa, where she married a Dutch convert recruit. They had three children - all of whom have since died.