BAGHDAD -- Gunmen attacked a prisoner convoy north of Baghdad on Thursday, setting off a gunbattle with troops in which scores of prisoners and eight soldiers were killed, brutally underscoring Iraq's instability as lawmakers convened to elect a new president.
U.N. chief Ban Ki-moon arrived in Baghdad earlier Thursday, urging lawmakers to "find a common ground" so they can address the crisis sparked by the rapid advance of the Islamic State extremist group and allied Sunni militants across much of northern and western Iraq last month.
At a press conference with embattled Prime Minister Nouri al-Maliki, Ban said Iraq is facing an "existential threat," but one that could be overcome if it forms a "thoroughly inclusive government."
"I am deeply saddened by the senseless death of so many Iraqi people," Ban said. He added that political leaders in Baghdad and the largely autonomous Kurdish region have a "clear responsibility" to work together to protect their citizens.
The dawn attack began with militants firing mortar rounds on Iraqi army bases in the town of Taji, where suspects were being held on terrorism charges, prompting authorities to evacuate the facilities, fearing a jailbreak, officials said.
As the convoy traveled through a remote area nearby, roadside bombs went off and militants opened fire. The ensuing battle left 52 prisoners and eight soldiers dead, with another eight soldiers and seven prisoners wounded, they said. It was not immediately clear if the prisoners were killed by soldiers or militants, or if the Islamic State group was involved.
The town of Taji is located some 12 miles north of the capital.
The officials - two policemen, an army officer and a medical official - spoke on condition of anonymity as they were not authorized to release information.
Islamic State militants have staged several jailbreaks, including a complex, military-style assault on two Baghdad-area prisons in July 2013 that freed more than 500 inmates.
Apparently fearing a repeat of the incident, Shiite militiamen killed nearly four dozen Sunni detainees last month in the town of Baqouba northwest of Baghdad when the facility where they were being held came under attack, according to a report by Amnesty International.
The report documented a "pattern of extrajudicial executions" of mainly Sunni detainees by forces loyal to the Shiite-led government, both in Baqouba and in the north, basing its conclusions on interviews with survivors and relatives of those killed.
The rapid advance of the Islamic State group, which captured Iraq's second largest city of Mosul and declared a self-styled Islamic Caliphate straddling the Iraq-Syria border, has plunged Iraq into its worst crisis since U.S. troops withdrew in 2011.
More than a million Iraqis have been displaced this year, many of them fleeing violence brought on by this latest wave of violence, according to the U.N.
Ban strongly condemned the persecution of religious and ethnic minority groups by jihadi militants in Mosul and elsewhere in Iraq, and offered continued U.N. support to the refugees fleeing the violence.
Al-Maliki has come under increasing pressure to step aside, with critics accusing him of monopolizing power and alienating the country's Sunni and Kurdish minorities. He has vowed to remain in the post he has held since 2006, and his bloc won the most votes in April elections.
The vote for president -- a largely ceremonial post currently held by Kurdish leader Jalal Talabani -- was to take place later Thursday after being delayed the day before when the Kurdish bloc requested more time to select a candidate. They named former deputy prime minister Fouad Massoum as their candidate late Wednesday.
Under an unofficial agreement dating back to the 2003 U.S.-led invasion, the presidency is held by a Kurd while the prime minister is Shiite and the parliament speaker is Sunni.
Speaking alongside the U.N. secretary-general, al-Maliki said he is committed to quickly forming a government.
"Despite the fact that we have problems...we are moving at a confident pace to implement the mechanisms of the democratic work," al-Maliki said.
Associated Press writers Murtada Faraj and Vivian Salama in Baghdad contributed to this report.