DOHA, QATAR -- Acknowledging a military stalemate after nearly two decades of conflict, the United States on Saturday signed a peace agreement with the Taliban that is aimed at ending America's longest war and bringing U.S. troops home from Afghanistan more 18 years after they invaded in the wake of the Sept. 11, 2001, terrorist attacks.

The historic deal, signed by chief negotiators from the two sides and witnessed by Secretary of State Mike Pompeo, could see the withdrawal of all American and allied forces in the next 14 months and allow President Donald Trump to keep a key campaign pledge to extract the U.S. from "endless wars." But it could also easily unravel, particularly if the Taliban fail to meet their commitments.

"Today, we are realistic. We are seizing the best opportunity for peace in a generation," Pompeo said in the Qatari capital of Doha. "Today, we are restrained. We recognize that America shouldn't fight in perpetuity in the graveyard of empires if we can help Afghans forge peace."

Under the agreement, the U.S. would draw its forces down to 8,600 from 13,000 in the next three to four months, with the remaining U.S. forces withdrawing in 14 months. The complete pullout would depend on the Taliban meeting their commitments to prevent terrorism, including specific obligations to renounce al-Qaida and prevent that group or others from using Afghan soil to plot attacks on the U.S. or its allies.

The deal sets the stage for intra-Afghan peace talks to begin around March 10, with the aim of negotiating a permanent cease-fire and a power-sharing agreement between rival Afghan groups. It's perhaps the most complicated and difficult phase of the plan. It does not, however, tie America's withdrawal to any specific outcome from the all Afghan talks, according to U.S. officials.

Pompeo said that "the chapter of American history on the Taliban is written in blood" and stressed that while the road ahead would be difficult, the deal represented "the best opportunity for peace in a generation."

At a parallel ceremony in Kabul, U.S. Defence Secretary Mark Esper and Afghan President Ashraf Ghani signed a joint statement committing the Afghan government to support the U.S.-Taliban deal, which is viewed skeptically by many war-weary Afghans, particularly women who fear a comeback of repression under the ultra-conservative Taliban.

President George W. Bush had ordered the U.S.-led invasion of Afghanistan in response to 9-11. Some U.S. troops currently serving there had not yet been born when al-Qaida hijackers flew two airliners into the twin towers of the World Trade Center, crashed another into the Pentagon and took down a fourth in Pennsylvania, killing almost 3,000 people.

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 U.S. 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 United States has spent nearly $1 trillion in Afghanistan, two-thirds of that on defence, most of it for its own soldiers but also for the Afghan Security Forces. More than 3,500 U.S. and coalition soldiers have died in Afghanistan, more than 2,400 of them Americans.

But the conflict was also frequently ignored by U.S. politicians and the American public as the memory of the attacks on that crisp, sunny morning faded, despite having changed how many Americans see the world.

While Pompeo attended the ceremony in Qatar, he appeared to avoid any direct contact with the Taliban delegation. The deal was signed by U.S. peace envoy Zalmay Khalilzad and Taliban leader Mullah Abdul Ghani Baradar, who then shook hands. Members of the Taliban shouted "Allahu Akhbar" or "God is greatest." Others in attendance, including the Qatari hosts, applauded politely.

"We are committed to implementing this agreement," Baradar said in brief comments. "I call on all Afghans to honestly work for peace and gather around the table for peace negotiations."

Some Taliban celebrated the deal as a victory. "Today is the day of victory, which has come with the help of Allah," said Abbas Stanikzai, one of the Taliban's lead negotiators.

Pompeo told reporters after the signing that he was still moved by the Sept. 11 attacks and proud of what the U.S. had accomplished in Afghanistan.

Meanwhile in Kabul, in a rare show of unity, Ghani sat beside his chief political rival Abdullah Abdullah at a ceremony with Esper and NATO Secretary-General Jens Stoltenberg that included a declaration between the Afghan government and the United States intended to show U.S. support for Afghanistan.

For Afghanistan's government which has been deeply criticized by its political opponents, including Abdullah, the real job ahead will be cobbling together a negotiating team to sit across from the Taliban. The talks are to determine the face of a post-war Afghanistan.

Those negotiations, to be held in Oslo, Norway, are expected to begin around March 10. The Taliban have made it clear they expect the Afghan government to release their 5,000 prisoners before the start of negotiations. Around that time, the Taliban are to release 1,000 government security forces. Until now the government has not agreed to the prisoner release which could unravel intra-Afghan negotiations before they even get started.

Esper warned the road ahead was a long one and would not be without its challenges. "This is a hopeful moment, but it is only the beginning, the road ahead will not be easy."

Trump has repeatedly promised to get the U.S. out of wars in the Middle East, and the withdrawal of troops could boost his re-election bid in a nation weary of involvement in distant conflicts.

He has approached the Taliban agreement cautiously, steering clear of the crowing surrounding other major foreign policy actions, such as his talks with North Korea.

Last September, on short notice, he called off what was to be a signing ceremony with the Taliban at Camp David after a series of new Taliban attacks. But he has since been supportive of the talks.

It's 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 administration, but it remains a deeply conservative country, with women still struggling for basic rights.

There are currently more than 16,500 soldiers serving under the NATO banner, of which 8,000 are American. Germany has the next largest contingent, with 1,300 troops, followed by Britain with 1,100.

In all, 38 NATO countries are contributing forces to Afghanistan. The alliance officially concluded its combat mission in 2014 and now provides training and support to Afghan forces.

The U.S. has a separate contingent of 5,000 troops deployed to carry out counter-terrorism missions and provide air and ground support to Afghan forces when requested.

Since the start of negotiations with the Taliban, the U.S. has stepped up its air assaults on the Taliban as well as a local Islamic State affiliate. Last year the U.S. air force dropped more bombs on Afghanistan than in any year since 2013.

Seven days ago, the Taliban began a seven-day "reduction of violence" period, a prerequisite to the peace deal signing.

------

Gannon reported from Kabul, Afghanistan. Associated Press writers Rahim Faiez and Tameem Akhgar in Kabul, Lorne Cook in Brussels and Joseph Krauss in Jerusalem contributed.