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 standoff, with demonstrators drawn largely from Iranian-backed militias, 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.