The High Court in Kenya has blocked the government's bid to close the largest refugee camp in the world.
A directive to shut the Dadaab camp and forcibly repatriate about 260,000 Somali refugees living there was issued last year.
The deadline for its closure had been extended until May, but a high court judge ruled the decision was tantamount to an act of group persecution.
The government had argued it was an issue of security.
It said that attacks on its soil by the Somalia-based al-Shabab group had been planned in the camp.
Dadaab was set up in 1991 to house families fleeing conflict in Somalia, and some people have been living there for more than 20 years.
The Kenya National Commission on Human Rights and lobby group Kituo Cha Sheria challenged the decision in court, saying it was discriminative and contrary to international law.
‘The government's decision specifically targeting Somali refugees is act of group persecution, illegal discriminatory and therefore unconstitutional,’ Judge John Mativo said.
The orders are excessive, arbitrary and disproportionate, he said.
The BBC's Nancy Kacungira in the capital, Nairobi, says the government could still appeal against the court's decision, but the ruling means it has to start putting in place mechanisms that will restore regular operations at the camp.
Kenya's refugee department had already been shut, which meant that new-born babies were not being registered as refugees, Said Abuka, a community leader and refugee in Nairobi, told the Associated Press news agency.
Amnesty International welcomed the ruling, saying it affirmed Kenya's legal obligation to protect people who seek safety from harm and persecution.
‘Today is a historic day for more than a quarter of a million refugees who were at risk of being forcefully returned to Somalia, where they would have been at serious risk of human rights abuses,’ the rights group's Muthoni Wanyeki said in a statement.
Somalia's UN-backed government, with the help of a 22,000-strong African Union force, is battling Islamist group al-Shabab to regain control of the country.
But it has only managed to secure major towns.
The extremists, who impose a strict version of Islam in the areas they control, still mount deadly bombings and have carried out a string of attacks in neighbouring Kenya.
Kenya is currently building a fence along its 700km (435-mile) border with Somalia, but has completed only 4km so far.
Want stories like this in your inbox?
Sign up to exclusive daily email
More Stories from Africa