The US has signed a peace agreement with Taliban militants aimed at bringing an end to 18 years of bloodshed in Afghanistan and allowing troops to return home from America’s longest war.

Under the agreement, Washington would draw its forces down to 8,600 from 13,000 in the next three to four months, with the remainder withdrawing in 14 months.

The complete pullout would depend on the Taliban meeting commitments to prevent terrorism.

President George W Bush ordered the US-led invasion of Afghanistan in response to the September 11 attacks in 2001. Some US troops currently serving there had not been born when the World Trade Centre was attacked.

George W Bush. Credit: PA

It only took a few months to topple the Taliban and send Osama bin Laden and top al Qaida militants scrambling across the border into Pakistan, but the war dragged on for years as the US tried to establish a stable, functioning state in one of the least developed countries in the world.

The Taliban regrouped and currently hold sway over half the country.

The US spent more than 750 billion dollars, and on all sides the war cost tens of thousands of lives.

US secretary of state Mike Pompeo attended the ceremony in Qatar, where the Taliban have a political office, but did not sign the agreement. It was signed by US peace envoy Zalmay Khalilzad and Taliban leader Mullah Abdul Ghani Baradar.

The Taliban harboured bin Laden and his al Qaida network as they plotted, and then celebrated, the hijackings of four airliners that were crashed into lower Manhattan, the Pentagon and a field in western Pennsylvania, killing almost 3,000 people.

Mr Pompeo had privately told a conference of US ambassadors at the State Department this week that he was going only because President Donald Trump had insisted on his participation, according to two people present.

Afghanistan's Taliban delegation arrive for the agreement signing between Taliban and U.S. officials in Doha. Credit: AP

Dozens of Taliban members had earlier held a small victory march in Qatar in which they waved the militant group’s white flags, according to a video shared on Taliban websites.

“Today is the day of victory, which has come with the help of Allah,” said Abbas Stanikzai, one of the Taliban’s lead negotiators, who joined the march.

Mr Trump has repeatedly promised to get the US out of its “endless wars” in the Middle East, and the withdrawal of troops could provide a boost as he seeks re-election in a nation weary of involvement in distant conflicts.

Under the agreement, the Taliban promise not to let extremists use the country as a staging ground for attacking the US or its allies, but officials is Washington are reluctant to trust the Taliban to fulfil their obligations.

The prospects for Afghanistan’s future are uncertain. The agreement sets the stage for peace talks involving Afghan factions, which are likely to be complicated.

Under the agreement, 5,000 Taliban are to be released from Afghan-run jails, but it is not known if the Afghan government will do that.

There are also questions about whether Taliban fighters loyal to various warlords will be willing to disarm.

It is also not clear what will become of gains made in women’s rights since the toppling of the Taliban, which had repressed women and girls under a strict brand of Sharia law.

Women’s rights in Afghanistan had been a top concern of both the Bush and Obama administrations, but it remains a deeply conservative country, with women still struggling for basic rights.