BAGHDAD -- Iraqi lawmakers elected a veteran Kurdish politician as the nation's new president on Thursday, hours after an attack on a prison convoy killed dozens of people, brutally underscoring the challenges faced by the country's leaders as they struggle to form a new government.

The 76-year-old Fouad Massoum, one of the founders of the Patriotic Union of Kurdistan party led by Iraq's previous President Jalal Talabani, accepted the mostly ceremonial position after winning two-thirds of the votes in parliament, noting the "huge security, political and economic tasks" facing the next government.

Massoum's election comes as Iraq is facing its worst crisis since the 2011 withdrawal of U.S. troops amid the blitz offensive last month by al Qaeda breakaway Islamic State group that captured large swaths of land in the country's west and north, including Iraq's second largest city of Mosul. The militants have also seized a huge chunk of territory straddling the Iraq-Syria border, and have declared a self-styled caliphate in the territory they control.

Iraq's large, U.S.-trained and equipped military melted away in the face of the militant onslaught, sapping morale and public confidence in its ability to stem the tide, let alone claw back lost turf.

Hours before Massoum was elected, militants fired mortar shells at an army base where suspects facing terrorism charges were being held in Taji, about 20 kilometres north of Baghdad. Fearing a jailbreak, authorities evacuated the facilities, officials said.

But as the prisoners were being bussed through an area nearby, militants attacked again, this time with roadside bombs, igniting a gunbattle that left 52 prisoners and eight soldiers dead, the officials said, adding that another eight soldiers and seven prisoners were wounded.

It was not immediately clear if the prisoners were killed by soldiers or militants, or if the Islamic State group was involved. No one immediately claimed responsibility on the bus attack.

In the past, Islamic 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.

The vote for president -- a post previously held by ailing Kurdish leader Talabani -- is widely viewed as a step toward achieving consensus among political rivals, seen as necessary for tackling the deteriorating security crisis.

Massoum is considered a soft-spoken moderate, known for keeping good relations with Sunni and Shiite Arab politicians.

He was born in what is now the Kurdish regional capital of Irbil in 1938. He entered politics when he was 16 years old, taking part in Kurdish-organized demonstrations. He joined the Kurdistan Democratic Party in 1964.

From 1973 to 1975, he was the Cairo representative of Kurdish rebels battling the Arab-dominated government in Baghdad, then went on to establish the Patriotic Union of Kurdistan with six other Kurdish politicians, including Talabani.

The next step in Iraq's political transition will be for Massoum, who has already officially assumed the title of president, to select a candidate for prime minister to try to form a new government.

Salim al-Jabouri, Iraq's new speaker of parliament, said at a press conference following the vote that Massoum should "use his constitutional powers to choose a candidate (for prime minister) from the largest political bloc."

Prime Minister Nouri al-Maliki's bloc won the most seats in April elections, but he has faced mounting pressure to step aside, with critics accusing him of monopolizing power and alienating the country's Sunni and Kurdish minorities, contributing to the latest unrest.

Al-Maliki has however vowed to remain in the post he has held since 2006.

UN 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 al-Maliki, Ban said Iraq is facing an "existential threat," but one that could be overcome if it forms a "thoroughly inclusive government."

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 UN 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.

More than a million Iraqis have been displaced this year, many of them fleeing the latest wave of violence, according to the UN.

Ban strongly condemned the persecution of religious and ethnic minority groups by jihadi militants in Mosul and elsewhere in Iraq, and offered continued UN support to the refugees fleeing the violence.

From Sweden, Foreign Minister Carl Bildt, said on his Twitter account that he warmly welcomes Massoum's election, which he hopes would "pave way for inclusive and broadly based government."

Associated Press writers Murtada Faraj, Sameer N. Yacoub and Vivian Salama in Baghdad, and Jan Olsen in Copenhagen, Denmark, contributed to this report.