The United Nations plans to replace an envoy who was expelled from Somalia after questioning the arrest of a political candidate with an extremist history, the world body said Friday.

The announcement came after three days of diplomatic back-and-forth over Somalia's expulsion of Nicholas Haysom, a longtime U.N. official who has also served as envoy to Afghanistan and South Sudan.

U.N. spokesman Farhan Haq said Secretary-General Antonio Guterres has full confidence in Haysom, deeply regrets Somalia's decision and spoke twice about it with Somali President Mohamed Abdullahi Mohamed. The world body also argues that its personnel can't be kicked out under the diplomatic doctrine that Somalia invoked.

Nevertheless, the secretary-general plans to appoint a new representative for Somalia, Haq said.

"It's crucial that the U.N. mission on the ground is able to go about its work," he explained. That work includes electoral support and other functions.

Somalia's U.N. mission didn't immediately respond to an inquiry about the developments.

The Horn of Africa nation announced Tuesday it was ordering Haysom to leave, saying he overreached and meddled in the country's internal matters.

In a letter to Somali officials, Haysom had raised concerns about the legal basis for the arrest of Mukhtar Robow, a former deputy leader of the deadly al-Shabab Islamic extremist group who defected and then ran for a regional presidency.

Robow was a leading candidate, and the election was days away, when he was arrested last month. Somali officials said he hadn't completed a defection process and had failed to renounce extremist ideology, among other allegations against him.

Deadly protests followed his arrest.

Haysom didn't immediately respond to an inquiry Friday, and he didn't directly address his ouster while briefing the U.N. Security Council Thursday on overall developments in Somalia.

He did, however, say that the violent fallout from Robow's arrest "does not bode well" for upcoming elections and that Robow's case could discourage other extremist group defectors from abandoning violence to pursue political change.

Meanwhile, Somali Ambassador Abukar Dahir Osman admonished the council that the U.N. and its representatives have "an obligation to respect their mandate and to not interfere in our internal affairs."

Somalia appreciates the U.N.'s help but "distinguishes between the institutions that we are part of and individuals' conduct that has a detrimental effect on our fragile nation," he said.

After three decades of civil war, extremist attacks and famine, Somalia established a functioning transitional government in 2012 and has since been working to rebuild stability.

Other countries have, at times, booted U.N. officials. Guatemala, for instance, announced in 2017 it was expelling the head of a U.N.-sponsored commission investigating alleged corruption in the Central American country.

After leaving for a work trip to the United States, he hasn't been allowed to return to Guatemala.

Guterres has rejected Guatemala's request to name a new chief for the commission.