In April, President Trump repeated his campaign promise to end U.S. military involvement in Syria. “I want to get out,” he said. “I want to bring our troops back home.”

In September, senior administration aides said at the time, the president was persuaded to change course. Some 2,000 U.S. troops would stay in Syria indefinitely, not only until the Islamic State was defeated, but also until a political solution to the overall Syria conflict was in place and, in a key part of Trump’s newly announced Iran policy, all Iranian forces and their proxies aiding Syrian President Bashar al-Assad had left the country.

On Wednesday, Trump set heads spinning within his own government and around the world by apparently reversing himself again. His decision was made on Tuesday, according to people familiar with the issue, following a small meeting attended only by senior White House aides and the secretaries of defense and state, most of whom, if not all, sharply disagreed.

“We have defeated ISIS in Syria, my only reason for being there during the Trump Presidency,” Trump announced in a Twitter post early the next morning. Stunned defense and diplomatic officials were left to confirm that Trump had ordered the immediate withdrawal of all U.S. forces.

In just the past week, senior officials — including the administration’s special envoys to Syria and the counter-Islamic State coalition — had said that defeating the last organized Islamic State pockets, in southern Syria near the Iraqi border, could be months away and that thousands of militants remained underground throughout Syria, waiting to reemerge.

The officials reiterated that the Syrian Democratic Forces, the Kurdish-dominated group of U.S.-trained and -equipped ground fighters, remained valued American allies who would not be deserted.

More broadly, they repeated in recent speeches and briefings, the ongoing U.S. troop presence was crucial leverage to assist U.N. efforts and to make the Iranians leave.

The multipronged new policy had been born after extensive criticism of the lack of a coherent administration strategy in Syria. While Trump was hailed early in his term for a cruise-missile strike in response to Assad’s suspected use of chemical weapons, his administration was frequently mired in debate over how to treat Russia’s support for the Syrian leader — whom Trump at one point reportedly suggested assassinating — and the president’s nagging public repetition of his desire to bring the troops home.

But the strategy had seemed to take hold, even with a skeptical Congress, with the final elimination of Islamic State strongholds and movement on the political front seen as just over the horizon. Iran, officials said, was being squeezed by U.S. sanctions and eventually would have no choice but to capitulate to U.S. demands in Syria.

The only potential upset in recent days was a threat by Turkish President Recep Tayyip Erdogan — who spoke with Trump at the Group of 20 summit three weeks ago and again on the telephone Friday — to send troops across the border to attack the U.S.-allied Kurdish forces in northeast Syria.

Officials familiar with the Friday call said that Erdogan, among other things, had stressed to Trump that the Syrian Kurds were terrorists — allied with Kurdish separatists in his own country — and asked why the United States was supporting them rather than its NATO ally. He noted that the Islamic State had been vanquished and questioned the need for an ongoing U.S. troop presence, saying that Turkish troops already massed on the Syrian border could handle any problem there.

The Erdogan call, many concluded as they tried to understand the reasoning behind a decision widely considered rash and unwise, was the only thing that could have provoked Trump. A senior congressional aide speculated that the call, and the withdrawal, were “definitely related.”

Jon Alterman, director of the Middle East Program at the Center for Strategic and International Studies, noted that the Syria decision also coincided with the administration’s notification to Congress late Tuesday that Turkey’s long-sought purchase of U.S.-produced Patriot missile defense batteries had been approved after a years-long battle over the terms of a deal between Ankara and Washington.

“It would be disturbing if a strategic gesture was made for commercial reasons,” Alterman said.

A senior administration official, made available to brief reporters on the Syria decision Tuesday, mentioned only Turkey among U.S. allies that Trump had informed of the decision in advance. “He informed President Erdogan as a neighbor of Syria,” the official said.

The Turkish government did not respond to a request for comment, although Foreign Minister Mevlut Cavusoglu reported a Wednesday telephone call with Secretary of State Mike Pompeo. Trump’s Syria announcement came amid other major news in Ankara — a Turkish victory at the World Trade Organization in a ruling against expanded U.S. aluminum and steel tariffs and the arrival there of Iranian President Hassan Rouhani for a visit with Erdogan.

Trump himself made no public appearances, canceling a scheduled meeting with Senate Foreign Relations Committee Chairman Bob Corker (R-Tenn.). Instead, he communicated via Twitter, where he posted a late-afternoon video of himself standing outside the Oval Office, saying that “it’s time for our U.S. troops to come home.” Fallen American warriors, he said, pointing at the sky, were “looking down” in approval.

But outside the confines of the White House, there was confusion and trepidation.

The administration official who spoke to reporters insisted that no one should be surprised at Trump’s decision because the president has been saying the same thing ever since the campaign. Asked about contradictions with statements from Trump’s own advisers in recent weeks and months, the official challenged “the notion that anyone within the administration was caught unaware.”

“It was the president’s decision to make, and he made it,” said the official, who indicated that Trump was never really behind the longer-term strategy announced in his name in September. “The president’s statements on this topic have been 100 percent consistent.”

Senior lawmakers of both parties said they had no warning the decision was coming. Many were sharply critical, saying it left the door open for Assad allies Iran and Russia, abandoned Kurdish allies, and undercut U.N. efforts.

A number of close U.S. allies who are members of the U.S.-led coalition against the Islamic State said they were not consulted and were given no prior warning. One European defense secretary put in a call Tuesday to Jim Mattis after hearing rumors of the decision and received a late-night call back from the defense secretary with confirmation. Gen. Joseph F. Dunford Jr., chairman of the Joint Chiefs of Staff, did not participate in the meeting with Trump and was in the dark until after it took place, according to several people familiar with the situation.

Although U.S. troops in Syria were not tasked with defending American officials or humanitarian aid organizations there, their presence was considered a safeguard against attack from various quarters. A State Department official said Wednesday that U.S. diplomats and aid personnel — their numbers already reduced following Trump’s earlier decision to eliminate American aid to reconstruct towns and cities where most of the anti-Islamic State fighting took place — would now be evacuated.

Mercy Corps, an international organization that provides aid to many of the 1.3 million people considered in need of humanitarian assistance in northeastern Syria, said it would probably have to reconsider its operations there.

“We’re not getting assistance in terms of logistics and safety,” country director Arnaud Quemin said in a telephone interview. “It’s just that the presence of the coalition in that part of the country . . . keeps the area calm,” adding that that presence allows organizations like his to “function without interference.”

Update: This story has been updated to reflect the scope of Mercy Corps’ humanitarian aid in northeastern Syria. Organization officials said that 1.3 million people, about half the total population of the region, are in need, not that it provides assistance to all of them.

Carol Morello contributed to this report.