When the grip of Iraq's Tehran-backed Shi'ite Muslim parties and militias threatened to slip following the killing of Iranian general Qassem Soleimani, they turned to an unpredictable rival.
At meetings in the Iranian holy city of Qom, they struck a deal with populist Shi'ite cleric Sayyed Moqtada al-Sadr, who commands a following of millions of Iraqis.
According to senior Iraqi officials and militia insiders, they promised Sadr a greater say forming a new Iraqi government and an augmented spiritual leadership role among Shi'ite paramilitary groups.
In return, he would draw on his mass following to weaken the anti-government and anti-Iran dissent that has erupted on Iraqi streets, and redirect the unrest towards demands for the withdrawal of US forces from Iraq, the sources said.
The agreement, sponsored by Iran and the Lebanese Hezbollah group, sought to preserve Shi'ite power in Iraq by uniting the factions of the Iran-backed groups with their rival Sadr.
The militias were in disarray after a US air strike killed Soleimani and Iraqi paramilitary chief Abu Mahdi al-Muhandis on Jan. 3.
Sadr was also off-balance. He had led anti-government unrest in previous years but did not control the latest round of anger against the political elite in spontaneous, leaderless demonstrations that broke out in October.
With the deal struck, Sadr - an opportunist who has at different times fought the United States, decried Iranian meddling, and supported then abandoned protests - now looks set to have a big say in the selection of a new government which must be picked by next Monday, officials and lawmakers say.
The previous cabinet resigned under protest pressure last year.
After Soleimani's killing, Iran and Hezbollah officials instructed pro-Iran militia leaders to put aside their differences with Sadr. The two sides had clashed in parliament and over government posts last year in an intra-Shi'ite power struggle.
They met in Qom - Sadr's new base as he pursues further religious study.
"Iran saw Sadr as the only solution to prevent the collapse of Shi'ite power under protest pressure and the weakening of factions it backs," an aide to Sadr who travelled to Qom said, requesting anonymity.
"Sadr has the popular base through which he might control the street. They wanted to use that."
Several paramilitary sources with knowledge of the meetings confirmed that the Iranian side asked Sadr, who comes from an influential clerical family with a history of leading anti-government insurrection under Sunni dictator Saddam Hussein, to use his following to control demonstrations.
In return Sadr demanded freedom to choose the next government and to be able to block the Iran-backed parties' preferences. "Iran did not oppose this," the aide to Sadr said.
Two paramilitary sources said Sadr demanded control of two ministries under Prime Minister-designate Mohammed Tawfiq Allawi, who has asked parliament to approve his government this week.
The militia groups also agreed Sadr could have an augmented symbolic role leading them in opposition to the United States, the paramilitary sources said.
"The resistance factions agreed Sadr would be the main voice in the resistance. The groups will in return support decisions he makes," said Nasr al-Shammari, a spokesman for the Iran-backed and US-sanctioned Nujaba paramilitary faction.
Shammari and two other paramilitary sources said Iraq's militia groups would consider a greater role for the head of Sadr's Peace Brigades militia, Abu Doaa al-Essawi, in coordinating their military strategy.
The groups have been seeking a replacement for Muhandis, expected to be a commander from the late militia leader's own group, Kataib Hezbollah.
Government officials and parliamentarians say Sadr will have significant influence over the cabinet line-up proposed by Allawi, which the premier has said will consist of independent candidates.
"If this cabinet passes it'll work well for Sadr. He prefers independents because they're weak and he can coopt them. He has a militia and the ability to intimidate people," a government official said.
Kurdish and Sunni politicians oppose the line-up being pushed by Sadr, fearing they stand to lose portfolios.
SHORT TERM GAINS
Sadr might make political gains in the short term. But his deal with Iran-backed groups has alienated many supporters.
"They stole our revolution, the militias and the Peace Brigades (Sadr's own militia)," protester Mahdi Abdul Zahra said as he watched police fire at his friends in Baghdad.
Sadr's followers, who had joined protests and sometimes protected demonstrators from violence by security forces and militiamen, abandoned protest camps on his instructions and subsequently attacked them.
Sadr has threatened to call a new "million-strong" rally to pressure parliament to approve the new cabinet - another move that could eclipse the original demonstrations that demand the fall of the entire Iraqi ruling elite.
Sadr issued an array of contradictory statements in early February. They called for a large protest demanding the withdrawal of US forces, then for his supporters to abandon anti-government protests, then rejoin them but cleanse them of alcohol and other vices.
Followers took over some protest sites and clashed with demonstrators, killing several people.
"We rejected Sadr's initial call to withdraw from protests and were angry with him," said Sheikh Shiyaa al-Bahadli, a tribal leader in Baghdad's poor Sadr City district.