BAGHDAD -- Iraq's secular former prime minister edged out Prime Minister Nouri al-Maliki in parliamentary vote totals released Friday, upsetting the Shiite-backed premier's re-election bid and earning the right to get the first shot at trying to form a government.

The narrow margin -- 91 seats for Ayad Allawi's coalition to 89 for al-Maliki's alliance -- sets the stage for months of political wrangling as the veterans of Iraq's young democracy attempt to win support for a majority coalition.

The results were based on numbers released by the election commission and compiled by The Associated Press. The commission released the seat allocation by province but did not include an overall number of seats won.

Within minutes of the results being released, al-Maliki appeared on a nationally-televised news conference and announced he would not accept the results. Al-Maliki, who appeared with his supporters lined up behind him, said he would challenge the results through what he described as legal process.

The vote totals were released just hours after a twin bombing near a restaurant in a city north of Baghdad killed at least 40 people, highlighting the violence many Iraqis fear could mar the postelection process as both sides scramble to forge enough alliances to assemble a governing coalition.

The top official for the United Nations in Iraq, Ad Melkert, said the UN believed the country's March 7 parliamentary elections were credible and called on all sides to accept the results. That thought was echoed by U.S. Amb. Christopher R. Hill and Gen. Ray Odierno, the top U.S. military official in Iraq, who praised what they described as a "historic electoral process," and said they support the finding of election observers who said there was no evidence of widespread or serious fraud.

Al-Maliki and his supporters in the State of Law coalition have previously called for a recount amid claims of vote rigging and fraud, but election officials refused.

The prime minister, who is fighting for a second four-year term, has widespread support from Iraq's Shiite majority but has tried to distance himself from his sectarian roots and portray himself as a nationalist who helped return stability to Iraq after years of violence. But his support for a ban of hundreds of candidates with alleged ties to Saddam Hussein's regime severely undercut any support he had from Sunnis, who felt the ban unfairly targeted their candidates.

Many Sunnis instead threw their weight behind Allawi, a secular Shiite who has built a broad coalition drawn from both Islamic sects. Allawi, who served as prime minister from 2004 to 2005, has used his anti-Iran rhetoric to appeal to Sunnis who are wary of Tehran's influence with their Shiite-majority government.

The tight race has set the stage for protracted political wrangling over forming a new government that could spark new fighting and complicate American efforts to speed up troop withdrawals in the coming months.

Friday's blasts underscored the security risks that remain in Iraq. The two bombs exploded near a popular restaurant in Khalis, a town 50 miles north of Baghdad, injuring dozens of people, said Maj. Ghalib Al-Karkhi, the police spokesman in Diyala province.

The bloc that wins the most seats gets the first stab at choosing the prime minister and forming the new government, which will run the country as U.S. forces drawn down from their current level of about 95,000 to 50,000 by the end of August. All U.S. forces are slated to leave Iraq by the end of 2011.