Islamist militants on Sunday shot to death two men accused by fighters of spying for the east African nation's weak government, a witness and militant member said.

Hundreds of Somalis watched as a firing squad arranged by al-Shabab — the militant group linked to al-Qaida that controls much of southern Somalia — shot the pair in the southern port town of Merca.

An al-Shabab official, Sheik Suldan Aala Mohamed, said the men admitted to spying.

After the deaths were ordered the two were shot by five masked men, said Mohamed.

"We were informed by the Islamists last night about the execution. Using loudspeakers they ordered residents to attend the event and watch it. Schools were also ordered to close," said Muhidn Dahir, a resident of Merca.

Factions of al-Shabab vow allegiance to al-Qaida and it has foreign fighters in its ranks, raising fears al-Qaida is seeking to make a base in Somalia. Al-Shabab has carried out several whippings, amputations and executions to enforce its own strict interpretation of Islam.

Somalia's government occupies only a few square blocks in Mogadishu and is propped by up thousands of troops from the African Union.

Last month two men were killed by a firing squad after being accused of spying for the African Union and the CIA.