SOMALIA'S government has ordered the United Nations envoy to the country to leave.

The move comes amid questions over the arrest of the al-Shabab extremist group's former deputy leader, who had run for a regional presidency.

The foreign ministry accused Nicholas Haysom, who arrived as envoy a few months ago, of diplomatic overreach that violated the Horn of Africa nation's sovereignty, declaring him "persona non grata".

Haysom had questioned the legal basis used in the arrest last month of Mukhtar Robow, a former al-Shabab spokesman who defected from the group in 2017, and whether UN-funded regional police in the South West were involved.

Ethiopian troops who are part of the African Union force in Somalia and Somali police arrested Robow days before the regional election in which he had been a leading candidate.

Deadly protests followed.

Robow was flown to Somalia's capital Mogadishu and has been held in a prison run by Somalia's intelligence agency.

A UN spokesman said they were not commenting now, and Haysom did not immediately respond.

Instead, Haysom and the spokesman for the UN secretary-general issued statements strongly condemning a mortar attack that hit the main UN compound in Mogadishu earlier on Tuesday.

Seven mortars landed inside the compound, leaving two UN staffers and a contractor with non-life-threatening injuries, Haysom's statement said.

Al-Shabab claimed responsibility.

Robow's arrest has been seen as a high-profile test of Somalia's treatment of defectors from the al Qaida-linked al-Shabab, Africa's most active extremist group.

Somalia's government welcomed his defection but not his popular candidacy to lead South West state, which took some officials by surprise.

In confirming Robow's arrest, Somalia's security ministry cited the federal government's ban on his candidacy, which said he had not completed the defection process.

The ministry also alleged that Robow had failed to renounce extremist ideology, and accused him of mobilising armed forces.

A joint statement by the United States, more than a dozen countries, the AU mission and the United Nations expressed concern after the arrest and protests.

Haysom's letter to Somali authorities questioned the legal framework of Somalia's defection process and asked how Somali authorities were able to detain Robow beyond the normal 48 hours.

The letter also questions the circumstances around civilian deaths during the protests and urges that they be "thoroughly and promptly investigated".