Somalia's al-Shabab Islamic extremist group says it has executed four men accused of spying for the British, Djibouti and Somali intelligence agencies.

Al-Shabab announced the killings on its Andalus radio station on Sunday, saying they were carried out by a firing squad in a public square in Kamsuma, a town in the Lower Jubba region.

The group's spokesman said its recent spate of attacks on hotels in the capital, Mogadishu, have been part of its drive against Somali intelligence agents and other government officials who he said were staying at the hotels.

"We don't attack every hotel in Mogadishu, but those specific ones (hotels) attacked by Mujahideen fighters have got specific features meriting them for attacks," said Sheikh Ali Mohamud Rage, on the rebels' radio station.

"A standard public hotel that civilians stay in is not protected by blast walls and soldiers. These apostates have got no distinct offices, and most of those men were killed at hotels they use as their offices," he said, apparently referring to Somali government officials.

Mogadishu has seen series of attacks by al-Shabab in recent weeks, highlighting challenges facing the Somali government, which said recently that it is considering imposing a state of emergency in Mogadishu to try to contain the attacks.

Rage also said that the recent withdrawal of Kenyan forces, who are part of the multinational African Union forces in Somalia, from villages were made as result of increased attacks and deaths and budget constraints faced by the Kenyan government.

Al-Shabab, which is al-Qaida's East Africa affiliate, has fought for years to impose a strict version of Islam in the Horn of Africa nation. Despite losing territory in recent years, the extremist group continues to carry out deadly attacks in many parts of the country, especially in Mogadishu. A truck bomb there in October 2017 killed more than 500 people.