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 says it will appeal against the ruling on security grounds.
It said that attacks on its soil by the Somalia-based al-Shabab group had been planned in the camp.
"The camp had lost its humanitarian nature and had become a haven for terrorism and other illegal activities," Kenyan government spokesman Eric Kiraithe said.
"The lives of Kenyans matter. Our interest in this case, and in the closure of Dadaab refugee camp, remains to protect the lives of Kenyans," he added.
Africa Live: More on this and other stories
Can Kenya close world's largest refugee camp?
Why is al-Shabab targeting Kenya?
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 discriminatory and contrary to international law.
Media captionInside Dadaab - the world's biggest refugee camp
"The government's decision specifically targeting Somali refugees is an 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 ruling means the government has to start putting in place mechanisms that will restore regular operations at the camp even though it will launch an appeal.
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.
Mr Kiraithe said since the closure of Dadaab was announced, more than 51,000 Somalis had voluntarily returned to Somalia.
"We are encouraged that Somalia continues to stabilise politically more so with the election of a new president," he added.
Somalia's 'Mr Cheese' president has a lot on his plate
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 militants, 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.