Iraqi forces took control of the contested city of Kirkuk from Kurdish authorities on Monday, as tensions over last month’s independence referendum in the Kurdistan region erupted into violence between two key US allies.

Thousands of civilians fled in panic from the city, which lies in the heart of a major oil-producing region and which both the Kurds and Iraq claim as their own.

After some initial clashes, troops led by Iraq’s elite, US-trained counterterrorism force moved into the city largely unopposed, seizing key military installations and oil fields, and hoisting the Iraqi flag in the center of the city.

One Kurdish health official told Agence France-Presse that at least 10 peshmerga fighters were killed and 27 wounded in battles Sunday night. But official numbers have yet to be released.

The scene in Kirkuk, Iraq, as Iraqi troops arrive to retake the city from Kurdish forces. Credit: Reuters

The fighting is the worst violence between Iraq and the Kurds since the reign of dictator Saddam Hussein and threatens to severely disrupt the coalition built by the US to defeat ISIS.

President Donald Trump addressed the crisis from the White House on Monday afternoon, saying the US is “not taking sides” in the fight.

“We’ve had for many years a very good relationship with the Kurds as you know, and we’ve also been on the side of Iraq, even though we should have never been there in the first place. We should never have been there. But we’re not taking sides in that battle,” he said.

As the US scrambled to de-escalate the fighting between two allies, the US-led, anti-ISIS coalition played down the clashes as a “misunderstanding” and said shooting began “as two elements attempted to link up under limited visibility conditions.”

But the Kurdistan regional government called it an “unprovoked attack ... using US military equipment, including Abrams tanks and Humvees.”

A statement from Kurdistan’s peshmerga general command described the operation as a “war against the Kurds,” and expressed resentment toward the US.

“The same weaponry was never supplied to peshmerga to fight ISIS when peshmerga was in dire need. Now, it’s turned against our people and forces,” it said.

‘The Jerusalem of Kurdistan’

Iraqi security forces and the peshmerga have fought alongside each other for two years against ISIS, backed with weapons and support from the US — weapons that were subsequently turned on each other.

So, how did two US-trained and equipped allies come to blows?

The status of Kirkuk has been a flashpoint between the Kurds and Iraq’s central government in Baghdad for decades. Tensions have risen dramatically since the semi-autonomous Kurdish region held a referendum on declaring independence from Iraq last month.

Related: Should Kurdistan break off from Iraq? We asked young voters.

Although the results were nonbinding, voters delivered a resounding victory in favor of secession, with more than 90 percent casting their ballots for “yes.”

The US, Turkey and Iran opposed the vote, while Baghdad declared it illegal and unconstitutional.

The Iraqi army controlled Kirkuk until 2014 when its soldiers fled from an ISIS advance toward the city. The peshmerga subsequently moved in and had held it since.

For many Kurds, taking control of Kirkuk was necessary for security and the righting of a historic wrong.

Kirkuk is about 60 miles south of Iraqi Kurdistan’s capital of Erbil. It’s home to a multiethnic population of roughly 1 million people — including Arab Muslims, Christians, Kurds and Turkmen.

Kirkuk is historically a Kurdish majority city. It later became a target of Saddam Hussein’s “Arabization” policy of moving Arab families into areas inhabited by Kurds and other minorities to tip the ethnic balance toward the Arabs.

But since Hussein was ousted, the Kurdish share of the population has grown larger as families have moved back to the areas where they once lived.

A sign of the importance Kurds attach to Kirkuk can be measured by its nickname: “the Jerusalem of Kurdistan.”

Kirkuk would also be crucial to the economy of an independent Kurdistan. The province provides about 340,000 barrels of oil per day — more than half the total output of the Kurdish region.

Related: Iraq forces take oil fields dashing Kurdish dreams

As far as Baghdad is concerned, the peshmerga taking control of the city was an opportunistic land grab and recapturing it was the federal government’s right to protect its sovereignty.

Prime Minister Haider al-Abadi said Iraq “acted to fulfill our constitutional duty and extend the federal authority and impose security and protect the national wealth in this city.”

After three years of fighting ISIS, Iraq’s economy is in desperate need of the oil Kirkuk provides. Abadi also has his eye on elections next year — losing Kirkuk would have damaged his standing immeasurably.

All ISIS, all the time

The speed with which the situation deteriorated appears to have taken the US by surprise. Some have speculated that a more active US role, earlier on, might have averted confrontation.

US Secretary of State Rex Tillerson made a last-minute effort to avert the Kurdish referendum that set the crisis in motion, but according to Bloomberg, the offer to the Kurdish President Masoud Barzani came too late.

Analysts say the US had a singular focus on defeating ISIS and didn’t plan for what came next.

“From 2003 to 2011, the US actively mediated in Kirkuk to prevent tensions between the different groups boiling over,” Emma Sky, who was the governorate coordinator of Kirkuk for the Coalition Provisional Authority of Iraq from 2003-04, told The World. “In recent years, the US focus has been largely limited to the tactical fight against ISIS — and not on the governance of Iraq.”

Sky added that “the US did not believe that Barzani would go ahead with the referendum.”

When fighting did break out, however, the US moved quickly to stop it from spreading.

“The lights have been burning in the State Department and the National Security Council all the way through the last week,” said Michael Knights, an analyst with The Washington Institute who has researched Iraq since the 1990s.

“Basically, the US position is quite tough on the Kurds. It’s supportive of the federal government bringing their forces back into the Kirkuk area, as long as they do not use force, and as long as they don’t take areas that were not in federal government hands in 2014. The idea is to turn the clock back to the way things were then.”

Related: How a Rex Tillerson oil deal nearly sparked an Iraqi-Kurdish war

Knights said when it became clear that Iraq would move to recapture Kirkuk, a deal was being discussed between Baghdad and Kurdish leaders to allow Iraqi forces to enter the city.

“No one was pulling the trigger on the deal. So, last night’s military action was quite a blunt Iraqi government effort to speed it along. The fighting was pretty limited, and that’s indicative of the fact that people understood that they were not there to fully resist this action,” he said on Monday.

But the apparent ease with which Iraqi forces entered the city has already driven a wedge between the two largest political parties in Kurdistan — which share control of the peshmerga.

In a statement issued by the peshmerga, the Kurdistan Democratic Party accused its rival, the Patriotic Union of Kurdistan, of “treason” for apparently withdrawing from its positions in the city and cooperating with Iraqi troops.

In just 24 hours, the unity on display during last month’s referendum has evaporated and the Kurdish government finds itself with few friends to call on for help.

President Barzani gambled that the goodwill his forces had built up by battling ISIS, and the fatigue of Iraqi forces due to that fight, would endure the rocky referendum process.

But, Sky said, “No Iraqi prime minister can afford to lose Kirkuk.”