The executions by firing squad took place at a square in the town of Bardhere, a Shebab stronghold in Somalia`s southwestern region of Gedo, late Tuesday, and came a week after the US said it had killed the Al-Qaeda-affiliated militants` intelligence chief in an air strike.
"One of the spies worked with the CIA and facilitated the killing of an Al-Shebab commander," a Shebab judge in the town said before the four were shot dead.
According to the judge, another one of those executed had been aiding US operations in Barawe, a port town and former Shebab stronghold that was captured last year by Somali and African Union forces, while the other two worked for Ethiopian intelligence and Somalia`s security agency.
"After hearing the charges against the four and their confession, the Islamic court sentenced them to the death penalty," the judge said.
A local witness, Ali Ronow, said hundreds of locals watched the execution.
"The men were blindfolded and shot from the back by a team of hooded gunmen," he said.
"All of them had their hands tied behind their backs when brought on a pickup truck and they were also hooded. They were shot indiscriminately with machine guns," said Muktarey, another witness.
Last week the United States conducted an air strike in Saacow, 320 kilometres (200 miles) west of the capital Mogadishu and in the Middle Juba region, and Somali officials said Shebab intelligence chief Abdishakur Tahlil was killed along with two other militants.
The Shebab`s former leader, Ahmed Abdi Godane, was also killed by a US air strike in September 2014.
Separately, a bomb stuck beneath a car in the capital Mogadishu wounded one, ripping off the leg of the driver as he travelled near the central K4 roundabout, police officer Awil Mohamed said.
"I saw his leg in a shoe left inside the car" said Farah Abdi, who was nearby the busy junction when the bomb exploded.
No one claimed responsiblity for the attack, but the Shebab have for years carried out a string of bombings and killings in their fight to overthrow the government.