A gathering of Somali leaders has overwhelmingly backed a new constitution, paving the way for a new government to be elected this month.
The vote came shortly after two bombers blew themselves up outside the meeting venue in the capital, Mogadishu.
Under a UN-backed plan, a new parliament will choose the next Somali leader on 20 August.
Somalia has been devastated by two decades of conflict and an al-Qaeda linked group controls many areas.
Its last functioning national government was ousted in 1991 and the anarchy has enabled both pirates and Islamist groups planning attacks around the world to set up bases in Somalia.
"This is an historic day - today we have witnessed the completion of a task that has been worked on for the last eight years," said Constitutional Affairs Minister Abdirahman Hosh Jabril.
Among the most significant elements of the new constitution are:
- A bill of rights, with everyone declared to be equal, regardless of clan or religion
- Islam is the only religion of the state, and no other religion can be propagated in the country - however, this was also the case previously
- Female genital mutilation - a widespread practice - is outlawed
- Citizens have the right to be educated up to secondary level
- A Truth and Reconciliation Commission is to be established
- Territorial disputes should be settled peacefully - Somalia has gone to war with both Kenya and Ethiopia over its claims to their Somali-inhabited regions
- Somalia will have a federal system - however the status of Mogadishu, the borders and distribution of power and resources between the regions are yet to be decided
BBC Somalia analyst Mary Harper says the constitution appears to exist in a parallel universe, a fantasy land, when compared with the reality on the ground in Somalia, with universal access to education and the end of female genital mutilation unlikely to happen anytime soon.
The constitution was supposed to have been put to a referendum but this was scrapped as it would be impossible to hold a vote in al-Shabab controlled areas.
Of the 645 members of the constituent assembly present, 621 backed the document and 13 voted against, while 11 abstained, reports the AP news agency.
Traditional elders will now select a new parliament, which must ratify the the document before it takes effect.
The 225 new MPs - far fewer than the current parliament - will then choose a new president, ending two decades of a series of transitional administrations.
However, the UN envoy to Somalia, Augustine Mahiga, has warned that the process to select the new parliament is already being undermined.
"We should not allow parliamentary seats to become commodities for sale or items for auction to the highest bidders at a time when we are seeking to reclaim the true stature of a dignified and respected Somali nation," he said.
A leaked UN report recently said that 70% of aid to the country was unaccounted for but the interim authority strongly denied that corruption was rampant.
The two bombers were shot as they approached the building but still managed to detonate their explosive vests, officials say.
No-one else was killed but at least one guard was injured, they say.
African Union troops and the UN-backed government have forced al-Shabab out of Mogadishu but the al-Qaeda linked group frequently stages attacks in the city.
The Islamist militant group still controls many areas of southern and central Somalia.
One of the country's most famous comedians, Abdi Jeylani Marshale, was buried on Wednesday - a day after being shot dead by two unidentified men.
It is not clear who was behind the killing but last year he was threatened by al-Shabab.
He used to make people laugh by impersonating Islamist fighters.