The Taliban has celebrated its total return to power in Afghanistan with gunfire and diplomacy, after the last US troops flew out of the country to end two decades of war.

The United States' longest military conflict drew to a close last night when its forces abandoned Kabul airport, where it had overseen a frenzied airlift that saw more than 123,000 people flee.

Taliban fighters then swept into the airport and fired weapons into the sky across the city in jubilation - an astonishing comeback after US forces invaded in 2001, weeks after the 11 September attacks, and toppled them for supporting al-Qaeda.

"Congratulations to Afghanistan ... this victory belongs to us all," Taliban spokesman Zabihullah Mujahid told reporters on the airport runway.

Mr Mujahid said the outcome was a "lesson for other invaders".

In Kandahar, the spiritual birthplace of the movement and the country's second-largest city, thousands of celebrating supporters swept onto the streets.

However, many Afghans are terrified of a repeat of the Taliban's initial rule from 1996-2001, which was infamous for its treatment of women and girls, as well as a brutal justice system.

The group has repeatedly promised a more tolerant brand of governance compared with its first stint in power, and Mr Mujahid persisted with that theme.

"We want to have good relations with the US and the world. We welcome good diplomatic relations with them all," he said.

He also insisted that Taliban security forces would be "gentle and nice".

The Taliban faces a daunting challenge of transforming from an insurgent group to a governing power, in a war-ravaged nation dependent on foreign aid.

The United Nations has warned of a looming humanitarian catastrophe, with food stocks running low because of disruptions caused by conflict as well as a severe drought.

Some Afghans appealed to the group to keep its promise of a softer rule.

Fawzia Koofi, a rights activist and former negotiator for the ousted government who has twice survived assassination attempts, called on the Taliban via Twitter to include all Afghans as it turns to ruling the country.

"Taliban, hear us out: we must rebuild together!" she wrote. "This land belongs to all of us."

Other activists struggled to find hope.

"If I let my thoughts linger on what we have lost, I will lose my mind," Muska Dastageer, who lectured at the American University of Afghanistan, said on Twitter.

The withdrawal came just before the 31 August deadline set by US President Joe Biden to end the war - one that claimed the lives of more than tens of thousands of Afghans and over 2,400 American service members.

The slightly early finish happened as a regional offshoot of the so-called Islamic State group, rivals of the Taliban, threatened to attack the US forces at the airport.

Thirteen US troops were among more than 100 people killed late last week when an IS suicide bomber attacked the perimeter of the airport, where Afghans had massed in the hope of boarding an evacuation flight.

Taliban members walk the tarmac as they secure the airport premises in Kabul

The US-led airlift began as the Taliban completed a rout of government forces around the country and took over the capital on 15 August.

Its victory came after President Biden withdrew nearly all US troops, but then was forced to send back about 6,000 more to conduct the airlift.

His critics continue to attack him for his handling of the withdrawal.

"We can't fight endless wars, but the scope & consequence of Biden's failure here is staggering," Republican Senator Rick Scott said on Twitter.

President Biden's top diplomat, Secretary of State Antony Blinken, was able to offer little more than stern words for the Taliban.

"Any legitimacy and any support will have to be earned," he said, as he announced the US had suspended its diplomatic presence in Kabul and shifted its operations to Qatar.

All eyes will now turn to how the Taliban handles its first few days with sole authority over the country, with a sharp focus on whether it will allow free departure for those wanting to leave - including some foreigners.

Mr Blinken said a small number of US citizens remained in the country - "under 200" but likely closer to just 100, and the UK said the number of its nationals there was in the "low hundreds".

Many thousands of Afghans who had worked with the US-backed government over the years and fear retribution also want to get out.

Western allies have voiced heartbreak in recent days that not all Afghans who wanted to flee could get on the evacuation flights.

NATO Secretary General Jens Stoltenberg said it was essential to keep Kabul airport open, "both to enable humanitarian aid to the Afghan people and also to make sure that we can continue to get people out".

The United Nations Security Council adopted a resolution requiring the Taliban to honour a commitment to let people freely leave Afghanistan in the days ahead, and to grant access to the UN and other aid agencies.

Talks are ongoing as to who will now run Kabul airport, which German Chancellor Angela Merkel warned was of "existential importance", as a lifeline for aid.