Get breaking news alerts and special reports. The news and stories that matter, delivered weekday mornings.

DOHA, Qatar — The U.S. invasion of Afghanistan started nearly 7,000 miles away on a sunny September morning when hijacked planes slammed into the twin towers of the World Trade Center in New York City, as well as the Pentagon and a field near Shanksville, Pennsylvania.

On Saturday, more than 18 years after the terror attacks of Sept. 11, 2001, the U.S. made a bid to end America's longest war.

Hundreds of miles from the battlefields of Afghanistan in a glitzy banquet hall in a five-star hotel in Qatar, the United States and the Taliban signed a landmark agreement that paves the way for U.S. troops to begin withdrawing from the poor and war-torn central Asian country.

"The Taliban will not allow any of its members, other individuals or groups, including Al Qaeda, to use the soil of Afghanistan to threaten the security of the United States and its allies,” the agreement states.

Under the pact, the U.S. would reduce its forces to 8,600 from 13,000 in the next three to four months. Remaining U.S. forces would withdraw in 14 months, although a complete pullout would depend on the Taliban meeting commitments to prevent terrorism.

U.S. special envoy Zalmay Khalilzad and the Taliban’s chief negotiator and one of its founders, Mullah Abdul Ghani Baradar, signed the agreement in Doha after more than a year of on-off formal talks.

Some in the room broke out in whoops, cheers and shouts of "God is Great" at the signing. The several dozen members of the Taliban exited the room after the ceremony beaming.

U.S. Secretary of State Mike Pompeo also attended the ceremony, but did not sign the "Agreement for Bringing Peace to Afghanistan," under which the Taliban pledged to enter into peace talks with Afghan government officials, representatives of the opposition, and members of civil society on March 10.

The U.S. committed to work with both sides in upcoming talks to secure the release of up to 5,000 prisoners held by the Afghan government and 1,000 prisoners held by the Taliban by the start of intra-Afghan peace talks.

Washington also agreed to lift U.S. sanctions on the Taliban later this year and to work with other members of the U.N. Security Council to remove sanctions against members of the Taliban within three months.

Speaking to reporters, Pompeo said the United States was "realistic" about the deal it signed, but was "seizing the best opportunity for peace in a generation."

Let our news meet your inbox. The news and stories that matters, delivered weekday mornings. This site is protected by recaptcha

He said that while he was still angry about the Sept. 11, 2001 attacks, the U.S. will not "squander" what its soldiers "have won through blood, sweat and tears."

Meanwhile, Secretary of Defense Mark Esper was in Kabul Saturday to meet with Afghan President Ashraf Ghani and leaders of NATO and the resolute support mission, which trains, advises and assists Afghan national defense and security forces. His aim there was to “further engage with the Afghan peace process,” he wrote in a tweet.

He later spoke to U.S. troops in the country, noting that some of them had not been born when the war broke out.

NATO Secretary General Jens Stoltenberg was also in Kabul on Saturday for another signing ceremony with Esper and Ghani.

Roots in 9/11

The deadliest terror attack on American soil, which killed nearly 3,000 people, prompted President George W. Bush to send the first of many waves of U.S. troops to Afghanistan, a country most Americans could not then spot on a map, in October 2001.

Their mission was to topple the Taliban regime after it sheltered Al Qaeda and its leader, Osama bin Laden, the architect of the Sept. 11, 2001, terror attacks. While the invasion sw­­iftly overthrew the militants, it also embroiled America in a deadly quagmire that has cost the lives of around 2,300 U.S. troops and wounded many thousands of others.

After 18 years, there are currently between 12,000 to 13,000 U.S. troops in Afghanistan, who advise Afghan forces and carry out counterterrorism operations against Al Qaeda and the Islamic State militant group.

President Donald Trump has repeatedly railed against America’s “endless wars” abroad and Saturday’s deal will give him a talking point in his bid for re-election.

America’s war in Afghanistan has spanned three U.S. administrations. Trump’s predecessor in the White House, Barack Obama, had tried to extricate U.S. troops from the country and even outlined a plan for a final exit. But Obama held off in the end amid concerns about the staying power of the Afghan security forces against a resilient insurgency.

The war has taken a devastating toll. Afghanistan currently tops the list of the world’s deadliest conflicts.

Since 2016, children have accounted for nearly a third of the estimated 11,000 civilian casualties every year in the conflict, according to Human Rights Watch. Since the United Nations began systematically documenting the impact of the war on civilians in 2009, it has recorded more than 100,000 civilian casualties, including more than 35,000 killed and 65,000 injured.

Between 2001 and October 2018, more than 58,000 Afghan security forces were also killed, according to a study by Brown University.

And nearly two decades after the U.S. invaded and billions of American taxpayer dollars later, the Taliban control, influence or contest nearly half of the country, according to the last reported numbers released by the Department of Defense in January 2019.

The Afghan government is perceived to be one of the world’s most corrupt and is currently facing its own political crisis as its rivals refute the results of September’s presidential elections, claiming that the polling was riddled with fraud.

And the status of women in Afghanistan — like so many other parts of Afghan life — is once again up in the air as a return to Taliban rule in the country could potentially jeopardize nearly two decades of progress for Afghan women.

But it was the security of Americans that Esper sought to highlight in Kabul on Saturday.

The defense secretary issued a message of thanks to the almost 800,000 U.S. troops that have served in Afghanistan since the war began. And he cautioned that it will take “many months” to withdraw fully. “Your mission is far from over,” he said, adding that there are still terror groups in Afghanistan.

“Your number one priority remains the protection of our people ... stay vigilant,” Esper said.

Saphora Smith and Mushtaq Yusufzai reported from Doha, Dan De Luce from Washington and Ahmed Mengli from Kabul.