Government forces only control a small part of the capital, Mogadishu
A powerful Sufi Muslim group has joined Somalia's government to tackle the al-Qaeda-inspired al-Shabab insurgents who control large parts of the country.
The deal, signed at the African Union headquarters in Ethiopia, is seen as a significant military boost for the beleaguered UN-backed government.
The Ahlu Sunna Wal Jamaa group controls several areas in central Somalia, where it has been fighting al-Shabab.
AU head Jean Ping welcomed the deal as a historic opportunity for peace.
He urged al-Shabab to lay down its weapons.
Al-Shabab and its allies control much of southern Somalia, while the government, backed by AU peacekeepers, controls only a few parts of the capital, Mogadishu.
"We have agreed to share power," said Somali Prime Minister Omar Abdirashid Ali Sharmarke at the signing ceremony.
Under the deal, Ahlu Sunna will be given five ministries and its forces will be incorporated into the government's security structures.
Ahlu Sunna and al-Shabab have very different interpretations of Islam.
However, some Ahlu Sunna factions have opposed the deal.