Iraqi leaders say the United States violated Iraqi sovereignty with attacks that killed 24 people in retaliation for the death of an American contractor.

Credit...Associated Press

Iraq has been caught for years in a tug of war between its two most powerful patrons, the United States and Iran. In recent months, public opinion began to tilt against Iran, with street protests demanding an end to Tehran’s pervasive influence.

But American airstrikes that killed two dozen members of an Iranian-backed militia over the weekend have now made Washington the focus of public hostility, reducing the heat on Tehran and its proxies.

Iraqi leaders accused the United States on Monday of violating Iraq’s sovereignty and expressed fear that increasing tensions between the United States and Iran could escalate into a proxy war on Iraqi soil.

Even the tenor of the street protests has shifted, as anti-Iranian slogans have given way to anti-American ones. Demonstrators and others attacked what they deemed to be America’s disproportionate response — the killing of 24 militiamen on Sunday in retaliation for the death of an American contractor on Friday.

By day’s end there were calls to end the “American occupation” and demands for the American military to leave Iraq.

For Iran, the reversal comes at an opportune moment, as it has faced pushback around the region and unrest and economic distress at home.

The American airstrikes on the militia’s bases in Iraq and Syria on Sunday wounded 50 people in addition to those killed, the militia, Kataib Hezbollah, said Monday.

The United States said the strikes were a reprisal for the more than 30 rockets Kataib Hezbollah launched against an Iraqi military base near Kirkuk on Friday, killing the American contractor and wounding four American and two Iraqi servicemen.

“What we did is take a decisive response that makes clear what President Trump has said for months and months and months,” Secretary of State Mike Pompeo said Sunday, “which is that we will not stand for the Islamic Republic of Iran to take actions that put American men and women in jeopardy.”

Despite the American justification, Prime Minister Adel Abdul Mahdi called the airstrikes “a violation of Iraqi sovereignty and a dangerous escalation and threat to the security of Iraq and the region.”

Iraq’s chief Shiite cleric, Grand Ayatollah Ali al-Sistani, condemned the American attack and warned that the government must “ensure that Iraq does not become a field for settling regional and international scores.”

Even if the American attack was “retaliation for illegal actions,” he said, the Iraqi authorities should deal with them, not the Americans.

Iraq’s Iranian-backed militias have increasingly posed a problem for both Iraq and the United States.

The militias arose to help defeat the Islamic State, a battle they effectively fought on the same side as the Americans. They now represent a powerful faction in Iraq, both militarily and politically, controlling a large bloc in Parliament.

While they are technically under the supervision of the Iraqi security forces, some have strong ties to Iran and operate with significant independence. As the Trump administration has imposed crippling economic sanctions on Iran, the militias have increasingly struck at American targets.

An American official said Sunday that the militias had carried out 11 attacks over the past two months on Iraqi bases and facilities housing American contractors and service members.

The group the United States accused of carrying out the deadly attack on Friday, Kataib Hezbollah, denied responsibility for it, a spokesman, Mohammed Muhi, said Monday.

And while the militia is closely tied to Iran, many Iraqis see it primarily as an Iraqi force and were angered by an attack on it by an outside power.

“We are talking about a foreign force attacking an Iraqi force,” said Maria Fantappie, the senior adviser on Iraq for the International Crisis Group.

While there has been some criticism of the militias’ attacks on Iraqi bases where Americans are stationed, most objections are now being leveled at the United States. The populist cleric Moktada al-Sadr, for instance, urged the militias to abandon “irresponsible actions,” saying he would work with them to use legal and political means to kick out the Americans.

Analysts also said the scale of the American attack — on five sites in two countries with two dozen people killed — made it likely that Kataib Hezbollah would feel compelled to respond and could rally anti-Americanism.

“Is that deterrence, or is this really risking the whole of the U.S. presence in Iraq?” asked Emma Sky, a senior fellow at the Yale Jackson Institute for Global Affairs.

The United States may have been trying to send a message that killing Americans was a red line not to be crossed, said Ranj Alaaldin, director of the Proxy Wars Initiative at the Brookings Institution in Doha, Qatar. But the toll of its attack was likely to yield “more intense and expanded operations” against Americans.

“What the U.S. intended and what the U.S. will get could be two very different things,” he said.

The militia, Kataib Hezbollah, which is separate from the Hezbollah movement in Lebanon, vowed unspecified “retaliation,” and Iran’s Foreign Ministry spokesman, Seyed Abbas Mousavi, said the United States “must accept full responsibility for the consequences of this illegal action.”

How Iran may respond is difficult to predict. If it chooses to escalate, it or its proxies could strike an array of American targets in Iraq, where there are troops on the ground and other Americans living and working.

The United States accused Iran this month of exploiting the chaos in Iraq to build up a hidden arsenal of short-range ballistic missiles there.

Iran could also work with its partners to attack American allies elsewhere in the region as it has tried to do in the past. Such targets have included Saudi Arabia, Israel and ships crossing the Persian Gulf. Iran’s Revolutionary Guards seized a ship in the Persian Gulf on Monday, the official IRNA news agency reported. The report did not say which country the ship belonged to.

Over the last several months, Iranian-backed militias have repeatedly poked at the Americans in Iraq, firing rockets into the Green Zone that were apparently aimed at the United States Embassy. The militias have also hit several Iraqi bases where Americans were billeted, including in Gayara, just south of Mosul, and in western Iraq near Al Asad Air Base.

“I think Iran was reading that Trump really wants out of the region and is not willing to respond militarily,” Ms. Sky said. So the Iranians have been “trying to figure out how far they could go.”

Until Friday, the militias had never killed an American.

A senior administration official in Washington, speaking on condition of anonymity according to White House rules, said that the airstrikes were intended to restore deterrence. The official said that Iran’s policy has been to conduct deniable attacks, a fiction that the United States would no longer allow.

The Trump administration placed economic sanctions on three militia leaders this month, including the leader of Kataib Hezbollah. The United States accused those militias of participating in an unprovoked attack on anti-government protesters that killed 15 people.

The American strikes in Iraq hit near a town on the Syrian border. The strikes in Syria were in the country’s eastern desert, where Iran supports forces fighting on behalf of President Bashar al-Assad in Syria’s civil war.

Analysts said that the American message was clear, but that it may have been overshadowed by the high death toll.

“That puts the ball back in Iran’s court,” said Mr. Alaaldin of Brookings. “But make no mistake, that ball will, for now, be played in Iraq’s political arena, where the United States is much weaker. Iran has a strategic game plan on the ground in Iraq aimed at protecting and enhancing its influence in Iraq. The Americans do not.”

Farnaz Fassihi, Falih Hassan and Michael Crowley contributed reporting.