Masked gunmen killed five Pakistani preachers Wednesday outside a mosque in Somalia following morning prayers, witnesses said.

Six gunmen with assault rifles and pistols stormed Tawfiq Mosque in Galkayo and forced six Pakistani preachers and a Somali man outside, said Ismail Mohamud Hassan, who was in the mosque at the time. The gunmen then opened fire, he said.

"Five of them died on the spot while two others were injured — one Pakistani and a Somali," Hassan told The Associated Press in a telephone interview from Galkayo, 470 miles northwest of the capital, Mogadishu.

Pakistan's acting ambassador in neighboring Kenya, Manzoor Chaudhry, confirmed that Pakistani nationals had been killed in Galkayo. The exact death toll was unclear because there have been conflicting figures from different sources, he said.

"It is a tragic incident. We are shocked. We are sad," Chaudhry told the AP.

The Pakistan High Commission in Kenya is also responsible for tracking Somali affairs because it is not safe to have an embassy in Somalia.

Galkayo District Commissioner Hussein Abdullahi said police are protecting the survivors of the attack and have surrounded the mosque.

It was not clear who was behind Wednesday's killing in this overwhelmingly Muslim country. Somali militiamen rarely target religious preachers, known as Tabliq.

"We are shocked. Why were these religious men, who are not involved in the conflict (in Somalia), targeted? It is a shame to Somalis," said Sheik Muqtar, a Galkayo resident.

Sheik Salad Dufan, a Galkayo resident, said Somali Islamic preachers who travel around the country usually preach at Tawfiq Mosque once a month.

"Sometimes they come with foreigners, mainly from Pakistan and Afghanistan," Dufan told the AP by phone.

Somalia has been ravaged by violence and anarchy since warlords overthrew dictator Mohamed Siad Barre in 1991, then turned on each other. A moderate Islamist was elected president in January in hopes that he could unite the country's feuding factions, but the violence has continued unabated.

The country's lawlessness has spread security fears around region and raised concerns that al-Qaida is trying to gain a foothold in the Horn of Africa. The anarchy also has allowed piracy to flourish off the country's coast.

The government and African Union peacekeepers hold only a few blocks of Mogadishu, while Islamic insurgents control much of the country and operate openly in the capital in their quest to impose a strict form of Islam in Somalia.

The U.S. considers one of the most powerful Islamist groups, al-Shabab, of being a terrorist group with links to al-Qaida, but al-Shabab denies that.