BAGHDAD — When the Iraqi military battled Kurdish forces this week to reclaim the contested city of Kirkuk, the spectacle of one American-backed ally fighting another with American-supplied weapons was not the only incongruous sight.

Another was the United States turning its back on a crucial ally in the fight against Islamic State, the Kurds, as Washington’s goals aligned with those of a regional nemesis, Iran.

While the military action in Kirkuk on Monday and Tuesday was carried out under the banner of the Iraqi military, the ground forces included Iranian-backed Shiite militias.

American officials, including President Trump, insisted that the United States was not taking sides in the dispute, but some analysts say the United States approved the Iraqi plan to enter Kurdish-held areas and that Iran helped broker the agreement with a Kurdish faction to withdraw its fighters from Kirkuk, allowing the Iraqi forces to take over largely unopposed.

“Abadi would not have attacked without informing the U.S.,” said David L. Phillips, a former State Department adviser who worked on Iraq for 30 years. “At a minimum, the U.S. knew that the attack was coming.”

Maria Fantappie, senior analyst for Iraq at the International Crisis Group, said, “The United States gave a green light, and that was essential.”

Iran’s goal, she said, was to insert Shiite militias into contested areas, and to divide the Kurds while solidifying Iranian influence over the Iraqi government.

Intentionally or not, the United States seems to have abetted that goal as it pursued its own aim of restoring Iraqi government authority to the disputed Kirkuk region. The United States, officials said, also declined to defend the Kurds to show its displeasure with them for rejecting an American request to cancel a referendum on independence from Iraq.

Supporters of the Kurdish Regional Government, the Kurdish enclave in northern Iraq, said they expected better from the United States after 1,700 Kurdish fighters died helping the Americans fight the Islamic State.

“I don’t want to use the word betrayal,” Vahal Ali, the communications director for the region’s president, said Wednesday, “but we definitely feel the United States has been negligent.”

He said the Kurds were “disappointed at how the United States looked at this.”

“Now they are giving Iraq to Iran as a present,” he added. “That’s as diplomatic as I can be.”

Joshua A. Geltzer, the former senior director for counterterrorism at the National Security Council, noted the irony of helping Iran just as Mr. Trump was assailing that country for sponsoring terrorism and threatening to withdraw from the Iran nuclear agreement.

“It seems like we just got out of the way as Baghdad rolled the Kurds, and that doesn’t feel right,” he said. “Plus, it makes little sense for an administration interested in getting tougher on Iran.”

The turn of events stems from the referendum the Kurds held three weeks ago, in which they voted overwhelmingly for independence from Iraq. Although the vote did not lead to a declaration of independence, it was vigorously opposed by nearly every power in the region.

Iran, which wields considerable influence in Baghdad, feared any move toward independence by the Kurds in Iraq would inflame separatist sentiments among its own Kurdish minority. The United States opposed the vote for, among other reasons, concern that it would rupture the coalition of Iraqi and Kurdish forces battling the Islamic State in Iraq.

The Kurdish Regional Government’s determination to go ahead with the referendum, chasing the dream of an independent Kurdish homeland, has backfired spectacularly.

In just two days this week, Iraqi troops took Kirkuk and most other contested areas that Kurdish forces have held since 2014, including oil fields that have provided the bulk of revenues for the Kurdish region.

The assault crushed Kurdish dreams of independence and raised serious questions about the political judgment and ultimate survival of the Kurdish leader, Massoud Barzani.

In retrospect, the depth of Mr. Barzani’s miscalculations are clear if still surprising. He badly overestimated his bargaining position with Iraq and underestimated the animosity his referendum engendered among his neighbors and allies, including the United States. After the referendum, American officials told Mr. Barzani that he had forfeited the good will of the United States.

For the last three years, the Kurdish militia known as the pesh merga was a crucial ally for the United States and Baghdad in the battle against the Islamic State, often proving more effective than the Iraqi army.

But less than two weeks after the Sept. 25 referendum, Iraqi forces drove Islamic State militants out of Hawija, their last major urban stronghold in Iraq and the last battle in which the pesh merga played a role. The fight has shifted to the western desert of Anbar Province, far from pesh merga positions in northern Iraq.

“Barzani had very little leverage before and he has absolutely no leverage now,” said Denise Natalie, a Middle East specialist at the National Defense University in Washington.

As Iraqi forces massed on the Kurdish border and Kurdish officials warned of an impending invasion, the Iraqi prime minister, Haider al-Abadi, insisted that Iraq did not plan to assault Kirkuk and dismissed reports to the contrary as “fake news.” American officials did not dispute his assertions.

Asked Wednesday whether the United States had approved Baghdad’s military plans, Heather Nauert, a State Department spokeswoman, said, “We have long called for a unified, democratic Iraq.”

The other surprise happened when Iraqi forces moved into Kirkuk and much of the pesh merga there, loyal to a Kurdish faction opposed to Mr. Barzani, had agreed to make way for the advancing Iraqi force.

Attention has since focused on a meeting on the weekend before the Iraqi assault between a prominent Iranian military commander, Maj. Gen. Qassim Suleimani, and members of that faction, the Patriotic Union of Kurdistan.

Gen. Khatab Omar, the police chief in Kirkuk, said General Suleimani held talks over the weekend with the P.U.K. in Kirkuk and in the Kurdish city of Sulaymaniyah.

Mr. Ali, the Kurdish government spokesman, said General Suleimani manipulated events to bring Shiite militias into areas vacated by the pesh merga.

When the first pesh merga withdrawals began on Friday from two districts south of Kirkuk in Shiite areas, Shiite militias accompanying Iraqi troops raised their flags over former Kurdish outposts.

There has been speculation that General Suleimani brokered the deal for the Kurdish forces to abandon Kirkuk, but Saadi Bira, a spokesman for the faction, the P.U.K., said local pesh merga commanders negotiated the agreement with the Iraqi government.

General Suleimani has been a pivotal, if shadowy, figure in Iraq for years. As the head of Iran’s foreign military operations, he directed Shiite fighters who killed hundreds of American troops in Iraq. In 2014, he wrote a provision incorporating Shiite volunteer fighters into the Iraqi armed forces, the so-called “Iraqi Hezbollah,” a reference to the Iranian-backed Shiite militia in Lebanon.

Jennifer Cafarella, senior intelligence planner at the Institute for the Study of War in Washington, said that an agreement organized by General Suleimani was “the most likely scenario in an event this strategically significant for Iran.”

“It does also seem likely that he was instrumental in forcing the Kurds to step down,” she added.

A spokesman for the American Embassy in Baghdad said the operation had involved Iraqi troops, apparently with the cooperation of Kurdish forces, regaining government control of disputed territory.

The Iraqi operation also forced a military confrontation between two American-backed allies. Both the pesh merga and the Iraqi military have received American training, intelligence and supporting airstrikes and artillery in several joint operations against the militants.

Senator John McCain, the Republican chairman of the Senate Armed Services Committee, was incensed. “The United States provided equipment and training to the government of Iraq to fight ISIS and secure itself from external threats — not to attack elements of one of its own regional governments, which is a longstanding and valuable partner of the United States,” he said in a statement.

Mr. McCain warned of “severe consequences” if American-supplied military equipment was destroyed in the operation. The Kurdish government said Monday that their fighters had destroyed five American-supplied Humvees used by the Iraqis.