Somalia has expelled the UN's top envoy, Nicholas Haysom, accusing him of "shaming" the world body by acting like the country's ruler.
Mr Haysom had raised concern about the killing of protesters allied with ex-militant Islamist Mukhtar Robow.
Security forces were allegedly involved in the deaths of about 15 of the protesters and the detention of about 300 people, the UN says.
Somalia's government is heavily dependent on foreign aid.
The UN provides training, uniforms and stipends to its security forces, who are battling militant Islamist group al-Shabab.
The al-Qaeda-linked group said it had fired mortars into the UN compound in the capital, Mogadishu, on Wednesday.
Three people were wounded in the attack, the UN said.
It condemned it as an "act of aggression", which may be in violation of international law.
Mr Robow, a former deputy leader of al-Shabab, defected from the group in 2017.
He announced last October that he intended to run for the presidency of Somalia's South West state.
However, the government banned him from contesting the poll and the security forces arrested him in early December.
The government said he had not "relinquished his extremist ideologies".
Mr Haysom wrote to the government on 30 December, questioning the legal basis for Mr Robow's arrest and the subsequent crackdown on his supporters demonstrating in Baidoa, the capital of South West state.
It is unclear whether the government replied to Mr Haysom's concerns, but Foreign Minister Ahmed Isse Awad said the UN envoy was "no longer welcome" in the country.
"Mr Haysom has violated diplomatic rules, and acted as though he is the head or the ruler of Somalia. He is a shame to the conduct of the UN and has violated the sovereignty and independence of Somalia," he told BBC Somali.
Mr Haysom is a South African and was a legal adviser to the country's first black President Nelson Mandela.
He later served as a UN envoy to Afghanistan, Sudan and South Sudan.
He was appointed as UN chief Antonio Guterres' special representative for Somalia in September 2018, and was assigned the task of helping to achieve peace in the country.
Somalia has been unstable since the fall of the Siad Barre regime in 1991.