Months of anti-government protests had centered on Iran’s influence in the country. A rocket attack and a series of airstrikes changed that. For months, furious protests have battered Iraq, driven by frustration at a dysfunctional economy, corruption and the pervasive influence of a foreign power: Iran.

Then a rocket attack killed an American contractor in Iraq, American airstrikes hit an Iranian-backed Iraqi militia, and Iraqis’ anger turned back on the United States, culminating with a break-in at its embassy compound in Baghdad on Tuesday. The airstrikes and the embassy break-in brought the United States to its most serious crisis in the country in years — and pulled it deeper into the volatile problems engulfing Iraq and its neighbor Iran.

Complicated at the best of times, the relations between Iraq, Iran and the United States are now even more fraught.

On Friday, more than 30 rockets were fired at an Iraqi military base near Kirkuk, in northern Iraq, killing an American civilian contractor and wounding four American and two Iraqi servicemen.

The United States accused an Iranian-backed militia, Kataib Hezbollah, of carrying out the attack. A spokesman for the militia denied its involvement. President Trump blamed Iran for the attack, writing Tuesday on Twitter, “Iran killed an American contractor, wounding many.”

The American military launched airstrikes against the militia over the weekend, killing 24 members in what Secretary of State Mike Pompeo called “a decisive response.” He said the United States would “not stand for the Islamic Republic of Iran to take actions that put American men and women in jeopardy.”

The United States and Iran are at longstanding odds — over influence in Iraq, Iran’s nuclear program and other issues — and tensions have spiked under the Trump administration, which pulled out of the 2015 nuclear accord and imposed punishing sanctions on Tehran.