DOHA, Qatar — The United States signed a deal with the Taliban on Saturday that sets the stage to end America’s longest war — the nearly two-decade-old conflict in Afghanistan that began after the Sept. 11 attacks, killed tens of thousands of people, vexed three White House administrations and left mistrust and uncertainty on all sides.

The agreement lays out a timetable for the final withdrawal of United States troops from Afghanistan, the impoverished Central Asian country once unfamiliar to many Americans that now symbolizes endless conflict, foreign entanglements and an incubator of terrorist plots.

The war in Afghanistan in some ways echoes the American experience in Vietnam. In both, a superpower bet heavily on brute strength and the lives of its young, then walked away with seemingly little to show.

American efforts to instill a democratic system in the country, and to improve opportunities for women and minorities, are at risk if the Taliban, which banned girls from schools and women from public life, become dominant again. Corruption is still rampant, the country’s institutions are feeble, and the economy is heavily dependent on American and other international aid.