Following Iraq's "inconclusive" national election on May 12, the United States tried hard to guarantee a second term for former Prime Minister Haider al-Abadi. Washington's special envoy Brett McGurk spent months talking Iraqi politicians into following the American blueprint aimed at isolating Tehran and "keeping anyone friendly to Iran out of power." But it was all in vain. The US failed to place its desired candidates in the important positions of prime minister, president and speaker of the parliament. Instead, Iran's Iraqi allies got their way. All three positions were filled with new faces who would not allow Iraq to turn its back to Iran.

Iran needed to make sure that Iraq's new government would not tilt towards Washington, and support the renewed US sanctions - the way Abadi did.Abadi had adopted the new set of US sanctions against Iran in August, in an attempt to countervail the electoral setback he faced in May and ensure a second term. And in doing so, he broke the Tehran-Washington compromise that brought him to power in 2014. Ironically, his anti-Iran posture received the strongest pushback from Iraq's political elite, and not Iran. He failed to control even MPs of his own coalition, and finally McGurk had to let him go. The Basra upheaval was the last nail in his premiership's coffin.

Importantly, a less US-inclined independent politician Adel Abdul Mahdi replaced Abadi. In a clear message to Iran, Abdul Mahdi opposed his predecessor's abidance by renewed US sanctions with an article in a Persian outlet, which he had been publishing in for years. Additionally, he was designated as PM by Barham Salih, the new Iraqi president whose election beyond Kurdish consensus was seen in Tehran as another blow to the special envoy McGurk and in extension to the Kurdistan Democratic Party (KDP)'s Masoud Barzani. The latter's nomination of Fouad Hussain was widely perceived as an effort to belittle Iraqi national politics.

How we got here

The May 12 election came amid political and social turmoil in Iraq, caused by the defeat of the Islamic State of Iraq and the Levant (ISIL, also known as ISIS), the Kurdish referendum and the re-taking of Kirkuk by Baghdad. Moreover, it was the first election after Baghdad's re-establishment of political ties with Riyadh - an actor keen to push back against Iran's elevated position in post-ISIL Iraq. Moreover, US was following a policy of maximum pressure on Iran, which had direct consequences for Iraq. Barzani was ready to jump on Washington's bandwagon because of his ire with Tehran's support for the re-taking of Kirkuk.

Against this backdrop, the Islah (Reform) coalition including Muqtada al-Sadr, Haider al-Abadi, Ammar Hakim and Iyad Allawi was announced in Baghdad's Babel Hotel on August 19. The alliance, with the support of Washington, had hoped to attract minor parties and secure majority in the Parliament. Three unexpected developments, however, made such an outcome impossible. First, the Fatah - State of Law Bina (Construction) coalition, led by former Prime Minister Nouri al-Maliki, announced its formation and declared that it holds the majority in the parliament. Second, smaller Sunni parties such as National Movement for Development and Reform refused to join the US-backed Islah coalition. And third, Abadi failed to control his own Nasr coalition, especially after the Basra upheaval in southern Iraq. Following these three developments, McGurk was forced to abandon his dream of keeping Abadi in office.

The race for the presidency was another story. Traditionally, the post is determined through a consensus within the Kurdistan Regional Government, however, the inability of the Kurds to unite behind a candidate moved their rivalry into Iraq's national politics - where Salih was elected decisively. Salih's election sets a new precedent in which the process of electing presidents is no longer a solely Kurdish matter, but rather a national one. Two hours into his presidency, Salih named Abdul Mahdi as the new prime minister.

Why did Iran win

Iran's recent victory against the US and Saudi Arabia in the Iraqi political landscape is first and foremost a result of the trust it has built in the country over decades. The US is not a trusted actor in Iraq due to its inconsistent policies. While the Shia believe Americans allowed Saddam's regime to crush them in 1991 and did nothing to stop ISIL's advances in 2014, the Sunnis consider the 2003 occupation as an "American betrayal". The Kurds, on the other hand, are angry over the US' inaction in the face of ISIL advances in 2014 and their loss of Kirkuk in 2017. Most Iraqi's however, view Iran as a consistent force. They may not support all of Tehran's policies, but they all trust its consistency. It took Tehran decades to build this trust, just as it took the US decades to lose it.

This trust played a significant role in the lead up to the election of a new prime minister. Despite their "nationalist" tendencies, both the Saairun (Marching) and Hikma (Wisdom) movements refused to support Abadi's decision to implement the new US sanctions on Iran. After months of intense negotiations, Saairun agreed to support Abdul Mahdi's candidacy alongside the Fatah coalition - knowing that Abdul Mahdi was Iran's favoured candidate. On Iraq's Sunni scene, despite US and Saudi efforts, many Sunni parties and figures chose to go against their post-2003 tradition and eventually also sided with the Iran-backed coalition. As a result, Mohammed al-Halbousi the favoured candidate of the Iran-backed coalition, was elected as speaker.

President Salih's election was also very much linked to Iranian support, with reports that McGurk tried to talk him out of candidacy. Turning the presidency into a national issue, on the other hand, was a strategic gain for Iran against the secessionist KRG.

In short, Iran managed to secure a supportive prime minister, a friendly president and, for the first time, helped elect a very supportive speaker of parliament. Above all, it managed to showcase that it remains a trusted ally for Iraq, even as the country carves its way into more diverse and less sectarian politics.Recent developments demonstrated that a cross-sectarian Iraq is not going to be an anti-Iran one. Therefore, Washington's plan to minimise Iran's influence in Iraq by "forming a strong Iraqi government on national basis" and a cross-sectarian coalition did not work. In other words, the "break down" of Iraq's "sectarian loyalties" is not happening at the expense of Iranian influence. This is a huge achievement for Tehran and and a bigg loss for Washington.

The views expressed in this article are the author's own and do not necessarily reflect Al Jazeera's editorial stance.