President Trump’s decision to withdraw U.S. troops from northern Syria, where they had long kept an uneasy peace among competing forces, left the region in upheaval Sunday and the administration scrambling to respond to fast-moving events.

In urgent meetings and telephone conferences, top national security officials studied often-conflicting accounts of what was happening on the ground. In public appearances, Cabinet secretaries denied that the United States had “abandoned” its Syrian Kurdish allies to invading Turkish ­forces and threatened severe sanctions against Ankara.

“This is total chaos,” a senior administration official said at midday, speaking on the condition of anonymity about the confusing situation in Syria.

Although “the Turks gave guarantees to us” that U.S. forces would not be harmed, the official said, Syrian militias allied with them “are running up and down roads, ambushing and attacking vehicles,” putting American ­forces — as well as civilians — in danger even as they withdraw. The militias, known as the Free Syrian Army, “are crazy and not reliable.”

At the same time, the official said, the Islamic State is active in the area, and there are reports that Russian and Syrian forces are moving in as well. “We obviously could not continue,” said the official, who called the situation “a total s---storm.”

Amid reports of Islamic State militants escaping prisons in the area, a U.S. official confirmed that the American forces had been unable to carry out plans to move several dozen high-value detainees to more secure locations, as first reported by the New York Times. One official said that multiple Kurdish-run detention facilities were now unguarded and that the U.S. military believed hundreds of detainees had escaped.

Trump decided late Saturday to remove all of about 1,000 U.S. troops from the area within weeks, as a Turkish invasion targeting U.S.-allied Syrian Kurdish fighters against the Islamic State expanded deep into Syrian territory, cutting U.S. supply lines and endangering American forces.

Before U.S. troops pulled out of northern Syria, clearing the way for a Turkish invasion, The Post went there and met Kurds who feared the looming assault. (The Washington Post)

A week ago, after a phone call with Turkish President Recep Tayyip Erdogan, Trump had ordered several dozen troops to pull back from the immediate border area when Turkey said an invasion was imminent.

In Congress, criticism of both Turkey and Trump was vocal and bipartisan.

“The weakness and incompetence that this president has shown when it comes to national security is stunning,” Sen. Jack Reed (R.I.), the ranking Democrat on the Armed Services Committee, said in a statement. Accusing Trump of “bending to autocrats,” Reed said that “instead of telling Erdogan to stand down, President Trump is in full retreat. It’s shameful.”

The senior official repeated denials that Trump had given Erdogan a “green light” to send his forces into Syria. “He did say, ‘Don’t do this or bad things are going to happen,’ ” the official said, such as canceling U.S. offers to improve trade between the two countries, preventing Turkey from rejoining the F-35 fighter jet program, and revoking an invitation for Erdogan to visit the White House next month.

Trump did not say “don’t do this because our troops would bomb” Turkish forces, because such action is deemed illegal unless conducted in self-defense, the official said.

Defense Secretary Mark T. Esper, speaking on “Fox News Sunday,” said that Turkey was fully committed to its operation regardless of what the United States did, and that the administration did not want to go to war with a long-standing NATO ally.

Asked whether he thought Turkey seemed like much of an ally now, Esper said he did not.

“I think Turkey, the arc of their behavior over the past several years, has been terrible,” he said. “I mean, they are spinning out of the Western orbit, if you will.”

The perception that U.S. troops were in increasing danger as Turkey and its allies attacked border towns and continued its advance led to the new withdrawal announcement, U.S. officials said. Turkey launched multiple artillery rounds Friday near a U.S. Special Operations outpost in the area, despite knowing its location, officials said.

While relatively few in number, U.S. troops in northern and eastern Syria, along with smaller French and British contingents, have aided and directed the Kurdish-led ground fight against the Islamic State for several years and provided a symbolic bulwark against interference by the Syrian government and outside forces.

With their departure, the many players in Syria’s overlapping conflicts scrambled for an advantage as civilians fled the fighting and an unknown number of Islamic State prisoners reportedly escaped.

Russia- and Iran-aided Syrian government forces, long held in abeyance by informal lines of control bordering Kurdish-held and U.S.-supervised areas in Syria, quickly seized the opportunity. The Kurdish fighters, outmanned and outgunned by the Turks and their Syrian militia allies, announced late Sunday that they had invited Syrian government forces into towns and cities that have been under their control for years.

“We don’t want the Russians and Syrians in there, but obviously we understand why they reached out,” the senior administration official said.

The official said long-standing Syria air deconfliction contacts with Russia were continuing, but that “we haven’t been in contact in any way, shape or form to invite them in or share views on the Near East with them.”

As the situation continued to evolve, U.S. officials planning a response focused on sanctions, to be imposed against Turkey as early as Monday.

Treasury Secretary Steven Mnuchin, appearing on ABC’s “This Week,” said he spoke Friday with the Turkish finance minister, and State Department officials are also in touch with their Turkish counterparts. “They know what we will do if they don’t stop these activities,” he said. Last week, Trump threatened to “obliterate” the Turkish economy.

The administration believes that the sanctions — and the knowledge that Turkey does not want to contend with the Syrian Army or its Russian ally — are the best weapons to convince Turkey to “behave,” the senior administration official said.

U.S. demands of the Turks include a cease-fire, as well as reining in their Syrian militias, treating “people well” and pulling back to the agreed border of a safe zone in Syria negotiated with the United States in recent weeks, the official said. That border was limited to 8.6 miles deep, with agreement that the Turks could conduct “patrols” up to the east-west highway about 20 miles from the Turkish border.

Officials’ accounts of the current troop plan continued to differ Sunday. One official said the Pentagon had pulled all U.S. troops back from northern Syria and was consolidating the U.S. position at bases farther south, including near Deir al-Zour. Hundreds were expected to leave Syria within days. Another U.S. official said Trump ordered the Pentagon to withdraw all forces within 30 days, except for those in the country’s far southeast, where fewer than 150 U.S. troops are positioned at the Tanf garrison.

Tanf has long been associated more directly with the U.S. desire to halt Iran’s expansion in Syria than the counter-Islamic State mission. The base’s position along a highway that connects Damascus to Baghdad and Iran has been seen as an important deterrent to Iran’s ability to resupply its forces in Syria.

Officials said some Pentagon officials, both civilians and those in uniform, had begun by Sunday to more openly express their anger and disappointment at the administration’s response to the situation, and at the troubling events on the ground. They described the planning surrounding the remaining U.S. troops in Syria as even more chaotic and rushed than is being depicted by administration leadership.

Erdogan reacted angrily to the condemnation he has received from world leaders over the past few days, which has included threats of arms embargoes, economic sanctions and demands that Turkey negotiate with the Kurds to end the fighting in northern Syria.

“Yesterday at the German parliament, the minister of foreign affairs made a speech and he said they would stop selling weapons to Turkey,” Erdogan said Sunday. “I just talked to Chancellor [Angela] Merkel,” he added. “I asked her to explain something to me: ‘Are we NATO allies or not? Or is the terrorist organization now a NATO member?’ ”

Turkey considers the Syrian Kurdish fighters to be terrorists allied with Turkish Kurds who have been involved in a violent campaign for autonomy in that country.

“I don’t know what sort of prime ministers or statespeople they are,” he said. “How can you recommend sitting down at the same table with terrorists?”

Erdogan did not give any indication he intended to halt Turkey’s offensive. Turkish forces would press 20 miles into Syria, he said. “Until they leave the space, we will continue the operation,” he said, referring to the Kurdish-dominated Syrian Democratic Forces. “We will not let a terrorist state be established in northeast Syria.”

Trump has played down concerns about the crisis for days, saying Turkey will be responsible for any Islamic State fighters who might break free in the chaos.

On Sunday, he tweeted before departing for his golf course in Virginia that it was “very smart not to be involved in the intense fighting along the Turkish Border, for a change” and accused “those that mistakenly got us into the Middle East Wars” of pushing the United States to stay in the fight.

Trump added that the Kurds and Turks have been fighting for years, a reference to the decades-long Kurdish insurgency in Turkey.

“Others may want to come in and fight for one side or the other,” he said. “Let them! We are monitoring the situation closely. Endless Wars!”

He added in a later tweet that he was working with members of Congress to impose sanctions on Turkey.

“There is great consensus on this,” Trump said. “Turkey has asked that it not be done. Stay tuned!”

After he returned to the White House from the golf course in late afternoon, he tweeted a criticism of the Democrat-led impeachment inquiry, and later tweeted that Islamic State prisoners “will never come to, or be allowed in, the United States!”

Asked about Trump’s decision to play golf while much of his national security team was in crisis mode, the senior administration official said, “I can assure you, the president has been earning his money on the Syrian account in the last eight days.”

Fahim reported from Istanbul. Liz Sly in Beirut contributed to this report.