Attackers of the armed group al-Shabab stormed into the religious center of Sheikh Abdiweli Ali Elmi Yare in the north-central city of Galkayo in Mudug region, attacking the Sufi leader and his followers.
"The attackers used a vehicle loaded with explosives to make their way into the compound before shooting indiscriminately at people inside," Abdirahman Mohamed, a security officer, told AFP news agency.
"The targeted cleric was killed in the attack together with some of his followers," Mohamed said.
Captain Nur Mohamed, a police officer, told Reuters news agency that the security operation is now over.
"Three militants who stormed the center were also shot dead," he said.
Al-Shabab, an al-Qaeda linked armed group based in Somalia, took responsibility for the attack and put the number of dead at 26.
It said some soldiers who responded to the first attack also died as they tried to remove a second car bomb.
"We killed 26 people including the owner of the center, his followers, his bodyguards and soldiers," Abdiasis Abu Musab, al-Shabab's military operation spokesperson, said.