Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

JERUSALEM — Israel said on Monday it has canceled a plan to deport African migrants to Africa and reached an agreement with the U.N. refugee agency to send more than 16,000 to Western countries instead.

Other migrants, many of whom are seeking asylum, will be allowed to remain in Israel for at least the next five years, Prime Minister Benjamin Netanyahu's office said in a statement.

The fate of some 37,000 Africans in Israel has posed a moral dilemma for a state founded as a haven for Jews from persecution and a national home. The right-wing government is under pressure from its nationalist voter base to expel the migrants, while others are calling for them to be taken in.

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

In February, Israel started handing out notices to 20,000 male African migrants giving them two months to leave the country or risk being thrown in jail.

The Israeli government has offered migrants, most of whom are from Sudan and Eritrea, $3,500 and a plane ticket to what it says is a safe destination in another country in sub-Saharan Africa, which Israeli media reports identified as Rwanda.

But rights groups advocating on behalf of the migrants say that many of them fled abuse and war and that their expulsion, even to a different country in Africa, would endanger them further.

The groups had challenged the deportation plan in Israel's High Court, which on March 15 issued a temporary order that froze its implementation.

A migrant walks near the entrance of the Population and Immigration Authority in Bnei Brak, Israel. Nir Elias / Reuters

"Israel and the U.N. High Commissioner for Refugees have reached unprecedented understandings for the departure of at least 16,250 migrants ... to Western nations," the Israeli statement said, without naming the countries.

A UNHCR spokeswoman confirmed that an agreement had been reached but gave no details.

The U.N.'s refugee agency had urged Israel to reconsider its original plan, saying migrants who have relocated to sub-Saharan Africa in the past few years were unsafe and ended up on the perilous migrant trail to Europe, some suffering abuse, torture and even dying on the way.

A fence Israel has built over the past few years along its border with Egypt has all but stopped African migrants from entering the country illegally. Since 2005, prior to which the border had been porous, a total of 64,000 Africans had made it to Israel, although thousands have since left.