The United States ended its longest war in history, and its 20-year presence in Afghanistan, as the last U.S. aircraft took off at one minute before midnight from Kabul airport Monday carrying all remaining American troops and diplomats.

“I’m here to announce the completion of our withdrawal from Afghanistan,” the head of U.S. Central Command, Gen. Kenneth F. McKenzie Jr., said at a news conference about an hour later as the final C-17 plane cleared Afghan airspace. The last to leave, he said, were Maj. Gen. Christopher Donahue, the commander of the Army’s 82nd Airborne Division, and acting American ambassador Ross Wilson.

President Biden issued a written statement saying he would address the American people Tuesday afternoon. The statement said that the decision to end the final U.S. military mission, which evacuated more than 120,000 Americans, Afghans and others over the past several weeks, was the “unanimous recommendation of the Joint Chiefs and of all our commanders on the ground.”

Secretary of State Antony Blinken, in a formal address, said the U.S. diplomatic mission to Afghanistan would be transferred for the time being to Doha, Qatar. From there, he said, “we will continue our relentless efforts to help Americans, foreign nationals and Afghans” at risk “to leave Afghanistan if they choose,” as well as what he said would be ongoing humanitarian and counterterrorism operations.

He said fewer than 200 American citizens are believed to still be in Afghanistan.

Calling it a “massive military, diplomatic and humanitarian undertaking,” Blinken said the evacuation mission was “one of the most difficult in our nation’s history.”

Defense Secretary Lloyd Austin said in a statement released Monday night that he hoped all Americans share his pride in the U.S. troops and diplomats who “raced to help save lives” in Afghanistan in August.

Video filmed on Aug. 30 showed the last U.S. troops to leave Kabul, ending a 20-year war. (Reuters)

“Our service members secured, defended, and ran a major international airport,” he said. “They learned how to help consular officers screen and verify visa applicants. They provided medical care, food and water, and compassion to people in need. They flew tens of thousands of people to safety, virtually around the clock. They even delivered babies.”

The costs of the war were immense, lasting through four administrations — more than 2,400 U.S. military deaths and tens of thousands of Afghans killed, and trillions of defense and development dollars spent.

Yet at the end of the day, the final departure returned Afghanistan to the undisputed rule of the Taliban, the Islamic fundamentalist militants whom U.S. forces ousted from power in 2001 and battled for nearly two decades.

After years of ups and downs on the battlefield, and in the size of the American and allied forces, which had dwindled to a few thousand during the Trump administration, the end came quickly. In barely a month, the Taliban spread its control to all major cities, and the U.S.-backed Afghan government collapsed as President Ashraf Ghani fled the country.

The U.S. military has been in regular contact with Taliban officials since their arrival in Kabul two weeks ago, and McKenzie described their communications as “pragmatic” and “businesslike” as they did not contest U.S. control of the Kabul airport. He said Donahue had spoken with his Taliban liaison just before departure.

“They established a firm perimeter outside of the airfield to prevent people from coming onto the airfield during our departure, and we worked on that with them for a number of days,” he said. “They did not have direct knowledge of our time of departure — we chose to keep that information very restricted — but they were actually very helpful and useful to us as we closed down operations.”

As the last planes took off, the U.S. military had a protective fleet of aircraft overhead that included a mix of MQ-9 Reaper drones, B-52 bombers, AC-130 gunships and F-15 fighters, said a U.S. defense official familiar with the operation.

Once the Americans were gone, celebratory gunfire could be heard throughout Kabul.

“The last American occupier withdrew from [Kabul airport] at 12 o’clock and our country gained its full independence, praise and gratitude be to God,” Taliban spokesman Zabihullah Mujahid wrote on Twitter.

U.S. officials said that a total of 122,000 men, women and children — 79,000 of them on American military aircraft and the rest on charter and allied military flights — were flown out of the country in a heroic and unprecedented airlift, as the Biden administration struggled to meet its own Aug. 31 deadline. The White House has said the number included about 6,000 Americans who were evacuated or otherwise departed Afghanistan since Aug. 14.

But the administration acknowledged that many were left behind, including American citizens whom McKenzie estimated number in the “low hundreds,” and tens of thousands of Afghans who aided the U.S. and allied effort over the years but were unable or unwilling to breach the danger and chaos of reaching the airport.

“There’s a lot of heartbreak associated with this departure,” McKenzie said. “We did not get everybody out that we wanted to get out.”

“We maintained the ability to bring them in until immediately before departure,” he said. “But none of them made it to the airport.”

There were no American civilians aboard the final five planes to leave, which carried the last 800 or so of more than 5,000 troops Biden had sent to handle the evacuation, along with what McKenzie said was “sensitive equipment.”

He said other equipment had been “demilitarized” on the ground, sometimes with explosives. The equipment left behind included 70 mine-resistant vehicles, 27 Humvees and 73 aircraft, none of which are usable anymore, he said.

Deaths continued throughout the final days, including 13 U.S. service members and scores of Afghans killed in a deadly suicide attack at the airport Thursday, and as many as 10 Afghan civilians, some of them children, who died when a U.S. drone hit a vehicle in a Kabul neighborhood Sunday.

The drone strike was targeting the Islamic State-Khorasan, the group’s offshoot in Afghanistan. U.S. officials have said ISIS-K was responsible for the airport bombing and five rockets fired at the airport Sunday.

The Taliban is a sworn enemy of the Islamic State, although it has long and strong ties to al-Qaeda. The United States has said it will continue to conduct over-the-horizon counterterrorism operations against both groups.

Blinken did not say whether the United States would recognize the still-to-be-formed Taliban government. He said the American posture in the future would be rooted in U.S. national interest, in particular whether the Taliban helped secure and return U.S. hostages and “bring security to the country.”

“We will not do it on the basis of trust or faith,” he said. “Not on what the Taliban government says, but what it does.”

That includes, he said, cooperating in counterterrorism efforts, refraining from any reprisals inside Afghanistan and forming an “inclusive government.” He said that the United States would “continue humanitarian assistance to the people of Afghanistan,” but that aid would not go to the Taliban but through the United Nations and nongovernmental organizations. “We expect those efforts will not be impeded,” Blinken said.

He said that Ian McCary, the former No. 2 U.S. diplomat in Afghanistan, would lead the new U.S. Doha office, while John Bass, a former U.S. ambassador to Afghanistan assigned by Biden to handle the diplomatic side of the evacuation from Kabul, would continue heading a team to resettle Afghans who have left or may leave in the future.

The United Nations Security Council approved a resolution Monday to urge the Taliban to follow through on promises to allow Afghans to depart the country when they choose, calling on the militants to permit humanitarian activities and prevent extremists from launching attacks.

Thirteen nations, including the United States, as well as France and Britain — which sent troops to Afghanistan over the years along with Germany, Italy and many other NATO and non-NATO nations — voted in favor of the resolution. The measure also condemned last week’s deadly bombing at the Kabul airport and highlighted the need for a negotiated political settlement that would respect the rights of all Afghans. China and Russia abstained, criticizing the West’s handling of the exit from Afghanistan.

“By adopting this resolution, the Security Council has shown that the world expects the Taliban to live up to these promises today, tomorrow and after August 31st,” Linda Thomas-Greenfield, the U.S. ambassador to the United Nations, told reporters after the vote. “This is of the utmost importance to us.”

Thomas-Greenfield said the United Nations was disappointed by the abstentions by Moscow and Beijing.

During the meeting, Russian Ambassador Vassily Nebenzia said he abstained because the resolution did not include a passage about the fight against organizations including the Islamic State and the Islamic Movement of Uzbekistan. Nebenzia said such an omission underplayed those groups’ threats.

He also underscored the “negative impact” the evacuations had on Afghanistan’s economy.

“With this brain drain, the country will not be able to achieve its sustainable-development goals,” Nebenzia said.

Chinese Ambassador Zhang Jun said that the resolution would intensify tensions in Afghanistan and that the international community should respect the nation’s sovereignty and “right to determine their own future.”

Jun criticized the “hasty and chaotic” withdrawal of U.S. troops and said countries that were present in Afghanistan “should be responsible for what they have done in the past 20 years.”

Earlier in the day, U.N. High Commissioner for Refugees Filippo Grandi pleaded with the international community to remain focused on the plight of Afghan civilians, warning in a statement that “a far greater humanitarian crisis is just beginning” as evacuations end and “the tragedy that has unfolded will no longer be as visible.”

“The scenes at Kabul airport these past few days have sparked an outpouring of compassion around the world at the fear and desperation of thousands of Afghans,” Grandi said. “But when these images have faded from our screens, there will still be millions who need the international community to act. . . . When the airlift and the media frenzy are over, the overwhelming majority of Afghans, some 39  million, will remain inside Afghanistan. They need us — governments, humanitarians, ordinary citizens — to stay with them and stay the course.”

On Monday, the World Health Organization said it had delivered its first shipment of medical supplies to Afghanistan since the Taliban regained power. It was the first of three deliveries planned by Pakistani aircraft flown into the northern city of Mazar-e Sharif.

Although the air evacuations have ended, many Afghans are trying to cross land borders into neighboring countries, although Iran and Pakistan, which have hosted many of an estimated 3.5 million Afghans displaced during decades of war, have said they will admit no more refugees.

Alex Horton, Missy Ryan and Miriam Berger contributed to this report.