Iraq’s newly elected president, Kurdish politician Barham Salih, center, walks at the parliament in Baghdad on Oct. 2, 2018. (Karim Ammar/AFP/Getty Images)

Iraqis named a president and a prime minister designate Tuesday, capping five months of halting negotiations that played out amid widening popular unrest and an intensifying rivalry between the United States and Iran for influence over Iraq’s leadership.

Within an hour of Iraq’s parliament electing veteran Kurdish politician Barham Salih as president, he announced that he had asked former oil minister Adel Abdul Mahdi to form the next government.

The selection of the men showed how the sectarian loyalties in Iraq’s Kurdish, Sunni Arab and Shiite Arab communities that have prevailed since the U.S. invasion in 2003 are breaking down, giving way to more-pragmatic coalitions that cut across sectarian lines.

Salih, 58, was chosen as president, a largely ceremonial post, after trouncing his Kurdish rival by a vote of 219 to 22, signaling overwhelming support among the legislature’s Arab majority for Salih’s brand of conciliatory politics.

Abdul Mahdi, 76, a Shiite with no recent party affiliation, will hold the vast majority of executive power, and his selection represents a compromise by the top finishers in May’s election, Iraqi and American officials said.

But the vote left no clear winner in the ongoing tussle between Iran and the United States to place their allies in Iraq’s key political posts as Washington seeks to isolate Tehran economically and politically.

Abdul Mahdi was backed by a faction of Shiite politicians led by populist cleric Moqtada al-Sadr, a critic of both the United States and Iran, whose ticket won the most seats in May’s election. Sadr’s pick was also supported by Kurdish and Sunni lawmakers and approved by Grand Ayatollah Ali Sistani, Iraq’s top Shiite cleric, according to an Iraqi lawmaker involved in the negotiations and a Western official.

A pro-Iran bloc of lawmakers, led by militia commander Hadi al-Amiri and former prime minister Nouri al-Maliki, tried to block Abdul Mahdi’s nomination but eventually dropped their opposition once they realized they did not have enough seats for a parliamentary majority. For letting Abdul Mahdi’s nomination proceed, the pro-Iran bloc may be rewarded with key ministerial posts such as oil, interior and defense.

“We are encouraged by the historic steps taken tonight,” said a senior U.S. official who spoke on the condition of anonymity because he was not authorized to comment publicly. “President Salih and Prime Minister-designate Abdul Mahdi are experienced leaders and well respected in Iraq and around the world.”

Abdul Mahdi will have 30 days to nominate a cabinet for parliamentary approval, according to Iraq’s constitution.

“Abdul Mahdi is a compromise candidate,” said lawmaker Youssef al-Kilabi, an ally of outgoing Prime Minister Haider al-Abadi, a favorite of U.S. officials. “He will run Iraq’s foreign policy in a balanced fashion, putting Iraq’s interests first. . . . He shares the same ideology as Abadi and Sadr.”

The new government will take over a country facing enormous challenges following a war against the Islamic State that caused billions of dollars in damage, left millions of people displaced and put a massive strain on Iraq’s oil-dependent economy. The government will also have to immediately address a persistent protest movement in Iraq’s Shiite heartland over poor public services, government corruption and frustrations with a political system that has favored narrow constituencies over basic governance.

The protests helped shake up the traditional sectarian alliances that previously had seen Shiites, Sunnis and Kurds fight it out for shares of state power. This year, lawmakers crossed sectarian lines to forge blocs that claimed a nationalist mantle. But the shifting political scene also has revealed fresh divisions within Iraq’s Kurdish autonomous region that came into focus during Tuesday’s election of the president.

Salih, who previously has served as deputy prime minister of Iraq and prime minister of the autonomous Kurdish Regional Government, is seen as a moderate figure who favors maintaining strong relations with both the United States and Iran while promoting Kurdish interests through cooperation with Iraq’s central government, rather than confrontation.

The urbane, British-educated politician is a favorite with Western diplomats and American military generals, and founded the American University of Iraq Sulaimani in 2007.

Salih could improve relations between Iraq’s Kurds and the central government in Baghdad after a Kurdish bid for independence last year threatened armed conflict. But his ascension to the presidency has deepened growing acrimony within the Kurdish Regional Government’s dynastic politics.

According to a power-sharing agreement put in place after the U.S. invasion, Kurds have traditionally nominated a single figure for the post of president from the Patriotic Union of Kurdistan — one of two dominant parties in the autonomous north of Iraq.

This year, amid competing visions for the future of the regional government, the Kurds failed to agree on a candidate, and the Kurdistan Democratic Party put forth Fuad Hussein, former chief of staff to powerful party leader Masoud Barzani.

Salih won the first round of voting by a margin of 165 to 89 — drawing condemnation from Barzani and provoking Hussein to drop out of the race. But the vote went ahead anyway, and lawmakers voted according to their preference in the absence of instructions from party leadership, marking a major departure from previous ballots in parliament.

Jalal Talabani, who held the position for two terms, used the office to mediate between Iraq’s central government and Kurdistan, while emerging as one of Iraq’s most important statesman.

His successor, Fouad Massoum, failed to match Talabani’s influence and turned the office into a “trivial position,” said Renad Mansour, an expert on Iraqi and Kurdish politics with Chatham House.

Talabani’s death last year accelerated the worsening of relations between Kurdish parties — and the Kurdish region’s ties with Iraq’s central government.

Many Kurds and Iraqis see Salih as a politician in Talabani’s mold, with the credibility to improve relations with Baghdad. But he is unlikely to quickly repair fraying ties in the Kurdish region.

“The conflict over the presidency reveals the lack of trust between the two parties,” Mansour said of the PUK and KDP.

The winner of the competition for supremacy between the two parties within the autonomous Kurdish region remains unclear, and Tuesday’s vote could have far-reaching consequences for the intra-Kurdish power struggle.

On Sunday, Kurds voted for the Kurdish region’s parliament, but results have not yet been released amid accusations of fraud and irregularities.