BAGHDAD — American troops advising Iraqi security forces in restive Anbar Province are sharing a base with odd bedfellows: an Iranian-backed militia that once killed United States soldiers. Both are fighting the militants of the Islamic State.

Here in the capital, though, Tehran and Washington still line up on opposite sides. The United States is urging the Shiite-dominated Iraqi government to do more to enlist members of the Sunni minority against the Islamic State. Shiite-led Iran and its proxies are thwarting that effort.

The dichotomy illustrates the complexities of the relationship between the United States and Iran in places like Iraq, where the interests of the two rivals clash and converge. Now, after a deal to limit Iran’s nuclear program cleared its biggest congressional hurdle last week, the United States will have to navigate an increasingly complicated regional maze with an Iran newly empowered by international legitimacy and relief from economic sanctions.

What is more, there are also indications that the contacts between the two countries that accompanied the nuclear negotiations have begun to produce more areas of limited collaboration in Iraq, Afghanistan and, to a lesser extent, in Yemen, adding to the tangle.