BAGHDAD — A secular rival edged Prime Minister Nouri al-Maliki’s Shiite alliance in final election results announced Friday, but instead of deciding who will govern Iraq as tens of thousands of U.S. soldiers head home, the virtual dead heat set up a test of raw power in the courts, in parliament – and on the streets.

Despite declarations from U.S. and United Nations officials that the elections had been fair, al-Maliki said he would go to court to demand a manual recount in parts of Baghdad and northern Iraq. His State of Law coalition, which won 89 seats, also pledged to bar some members of the rival Iraqiya list from parliament who have been accused of having been members of Saddam Hussein’s Baath Party.

Final results gave Ayad Allawi 91 seats in the 325-member parliament.

“These votes belong to the people,” al-Maliki said. “We have our suspicions. Let us recheck so there will be a peaceful exchange of power.”

Even before the final results were announced, some al-Maliki supporters had lashed out at the electoral commission and accused the CIA and State Department of planning to topple al-Maliki in favor of Allawi. Candidates from al-Maliki’s coalition spoke darkly of Shiite-dominated southern provinces loosening their ties to Baghdad if Allawi became prime minister. His coalition sponsored demonstrations in Baghdad and the southern provinces, where crowds shouted slogans demanding a recount and against the ousted Baath party.

The charges of fraud drew strong rebukes from the U.N.’s special representative in Iraq, Ad Melkert. U.S. Ambassador Christopher Hill and the U.S. military commander, Gen. Ray T. Odierno, issued a joint statement calling the elections fair and demanding parties stop casting doubt on the results.

“We encourage all political entities to conduct talks on the formation of the new government in a spirit of cooperation and respect for the will of the voters, and to refrain from inflammatory rhetoric or action,” it said.