(Adds details and background)
DJIBOUTI, Jan 31 (Reuters) - Moderate Islamist leader Sheikh Sharif Ahmed was poised to win the Somali presidency on Saturday after the prime minister pulled out of an election in a special parliament session in neighbouring Djibouti.
Meeting into the early hours, legislators went to a second round of voting after the withdrawal of premier Nur Hassan Hussein. He had been considered Ahmed's main rival for the presidency of Somalia, one of the world's most strifetorn states.
"I thank you very much and I am very hopeful everything will go smoothly and end as the people wish," Hussein said, shaking Ahmed's hand after his withdrawal.
The Islamist, who headed the sharia courts movement that ruled Mogadishu and most of south Somalia in 2006 before an Ethiopian military intervention, faced Maslah Mohamed Siad, son of ex-dictator Mohamed Siad Barre, in a second round of voting.
That was expected to end around 4 a.m. local time (0100 GMT), with a simple majority of votes among the 425 parliamentarians enough to win.
Analysts say that Ahmed will have the best chance of all the candidates to unite Somalis, given his Islamist roots and acceptability to other sides. Reconciling Somalia's 10 million people and stopping 18 years of bloodshed in the Horn of Africa nation would, however, remain a daunting task even for him.
Parliament was meeting in neighbouring Djibouti due to instability in Somalia.
In the first round of voting, among 14 candidates, Ahmed, from the opposition Alliance for the Re-Liberation of Somalia (ARS), was first with 215 votes.
He was followed by Siad, with 60 votes, and then Hussein with a surprisingly low 59.
Under a U.N.-brokered peace process, the new president is to fly at the weekend to an African Union (AU) summit in Ethiopia before returning to Somalia to lead a unity government.
In the past two years, more than 16,000 civilians and an unknown number of combatants have died during an Islamist-led insurgency against the government and its Ethiopian military allies.
One million people have been driven from their homes, and a third of the population is reliant on food aid in what aid agencies call one of the world's worst humanitarian crises.
Somalia has had no real central government since warlords ousted Mohamed Siad Barre in 1991.
Despite the withdrawal of Ethiopian troops earlier this week, and the Djibouti peace process intended to reconcile the government and opposition, hardline insurgents led by the Al Shabaab insurgent group are still fighting for power.
Al Shabaab has denounced the Djibouti election as meaningless. It captured the seat of parliament in the central town of Baidoa this week, meaning the government only controls a few blocks of Mogadishu with the help of African Union peacekeepers.
Former President Abdullahi Yusuf, accused by the international community of being an obstacle to peace, quit as president last month after four years in power.
Parliamentary speaker Sheikh Aden Madobe has been the interim president.
Many Somalis were sceptical that the Djibouti process would bring peace at last, saying whoever was elected president would still face a major threat from hardliners, and that an election abroad would lack legitimacy.
"The international community is electing a Somali president of their choice ... just like (President) Hamid Karzai of Afghanistan," said Mogadishu mechanic Mohamed Abdulle, 35.
Our Standards: The Thomson Reuters Trust Principles.