The Islamist Ennahda party was officially declared the winner of Tunisia's election, setting it up to form the first Islamist-led government in the wake of the Arab Spring uprisings.
But the election, which has so far confounded predictions it would tip the North African country into crisis, turned violent Thursday when protesters angry their fourth-placed party was eliminated from the poll set fire to the mayor's office in a provincial town.
Ennahda has tried to reassure secularists nervous about the prospect of Islamist rule in one of the Arab world's most liberal countries by saying it will respect women's rights and not try to impose a Muslim moral code on society.
The Islamists won power 10 months after Mohamed Bouazizi, a Tunisian vegetable seller in the town of Sidi Bouzid, set fire to himself in an act of protest that led to the fall of Tunisia's autocratic leader and inspired uprisings in Egypt and Libya.
We salute Sidi Bouzid and its sons who launched the spark and we hope that God will have made Mohamed Bouazizi a martyr, said Ennahda leader Rachid Ghannouchi, a soft-spoken Islamic scholar who spent 22 years in exile in Britain.
We will continue this revolution to realise its aims of a Tunisia that is free, independent, developing and prosperous in which the rights of God, the Prophet, women, men, the religious and the non-religious are assured because Tunisia is for everyone, Ghannouchi told a crowd of cheering supporters.
Announcing the results, election commission members said Ennahda had won 90 seats in the 217-seat assembly, which will draft a new constitution, form an interim government and schedule new elections, probably for early 2013.
The Islamists' nearest rival, the secularist Congress for the Republic, won 30 seats, the commission members told a packed hall in the capital, ending a four-day wait since Sunday's poll for the painstaking count to be completed.
Ennahda, banned before January's revolution, fell short of an absolute majority in the new assembly. It is expected to broker a coalition with two of the secularist runners-up and, with them, form a government.
The Islamists will get the biggest say on important posts. They have already said they will put forward Hamadi Jbeli, Ghannouchi's deputy and a former political prisoner, for the post of prime minister.
Tunisia's complex election system, which replaced the rigged, one-horse races conducted before the revolution, made it impossible for any one party to win a majority of assembly seats.
Ennahda lies at the moderate and liberal end of the spectrum of Islamist parties in the Middle East. Ghannouchi models his approach on the moderate stance of Turkish Prime Minister Tayyip Erdogan.
Secularists say the Islamists will try to impose an Islamic moral code on society but Ghannouchi has denied this. His officials say there will be no restrictions on foreign tourists -- a big source of revenue -- drinking alcohol or wearing bikinis on the country's Mediterranean beaches.
The party's victory is the first for Islamists since the Hamas faction won an election in the Palestinian Territories seven years ago.
It is a result which will resonate in Egypt, where a party with ideological ties to Ennahda is expected to do well in a multi-stage parliamentary poll that starts in November.
Thursday night's violence broke out in Sidi Bouzid, the birth-place of the revolution which ousted autocratic leader Zine al-Abidine Ben Ali.
Protesters there were angry that election officials had cancelled seats won by the Popular List, a party led by businessmen Hachmi Hamdi, over alleged campaign finance violations. The party is popular in Sidi Bouzid.
They have set fire to a large part of the mayor's office, and the police are nowhere to be seen, local resident Mehdi Horcheni told Reuters by telephone from the town.
He said elsewhere in the town, the protesters set fire to an Ennahda campaign office and a training centre, while police used tear gas in a failed attempt to disperse the crowd.
Another witness, Hafed Abdulli, said the crowd was burning tyres in the streets. People are protesting against the cancellation of the Popular List, he said.
The Popular List was running in fourth place in the election, according to preliminary results, before its seats were cancelled. The party's leader used to support Ben Ali and during the election ran a populist campaign heavily promoted on the British-based television station he owns.
The violence appeared confined to Hamdi's supporters, as the three main secularist parties have already accepted defeat and there were no reports of clashes in other towns.
There has been none of the violence that was predicted involving hardline Islamists who are more radical than Ennahda or the secularists who believe the election result will threaten their liberal lifestyles.
Ghannouchi and his party officials have issued a carefully-choreographed series of announcements designed to reassure sceptics that there is no need to fear an Islamist government.
Defying stereotypes about Islamists keeping women covered up, one of the party's most prominent candidates is a businesswoman who does not wear the Islamic veil, or hijab, and this week sang along to pop songs at a party rally.
Ennahda has also reached out to anxious investors by saying it will not impose Islamic banking rules. It says it is inclined to keep the finance minister and central bank governor in their posts when it forms the new government.
(Additional reporting by Abdelaziz Boumzar; Writing by Christian Lowe; Editing by Ralph Gowling)