(Updates with Obama announcement, further details)
By Steve Holland
WASHINGTON, May 27 (Reuters) - U.S. President Barack Obama on Tuesday outlined a plan to withdraw all but 9,800 American troops from Afghanistan by the end of the year and pull out the rest by the end of 2016, ending more than a decade of military engagement triggered by the Sept. 11 attacks on the United States.
The decision means that Obama will leave office in early 2017 having extricated the country from the longest war in U.S. history. He ended Washington's combat presence in Iraq in 2011.
Obama's White House Rose Garden announcement prompted criticism from Republicans that the hard-fought gains made against the Taliban could be lost in much the same way that sectarian violence returned to Iraq after the U.S. withdrawal.
Obama, who made a whirlwind visit to U.S. troops in Afghanistan over the weekend before American combat operations conclude at the end of 2014, appeared to anticipate concerns that he is abandoning Afghanistan. He said it is time for Afghans to secure their country.
"We have to recognize that Afghanistan will not be a perfect place, and it is not America's responsibility to make it one," Obama said.
Under his plan, 9,800 U.S. troops would remain behind into next year. By the end of 2015, that number would be reduced by roughly half.
By the end of 2016, the U.S. presence would be cut to a normal embassy presence with a security assistance office in Kabul, as was done in Iraq.
The 9,800 troops would take an advisory role backing up Afghan forces. They would train Afghan troops and help guide missions to rout out remaining al Qaeda targets.
Any U.S. military presence beyond 2014 is contingent on Afghanistan's government signing a bilateral security agreement with the United States.
Outgoing Afghan President Hamid Karzai has refused to sign it. But U.S. officials were encouraged that the two leading candidates in Afghanistan's presidential race, Abdullah Abdullah and Ashraf Ghani, have both pledged to sign quickly should they be elected in the second round of voting set for June 14.
Obama said the lengthy U.S. presence in Afghanistan is proof that "it's harder to end wars than it is to begin them."
"But this is how wars end in the 21st century: not through signing ceremonies but through decisive blows against our adversaries, transitions to elected governments, security forces who are trained to take the lead and ultimately full responsibility," he said.
While Americans have long since grown weary of a conflict in which nearly 2,200 U.S. troops have been killed, some Republicans greeted the news with skepticism.
They continued a drumbeat of criticism of the president's handling of foreign policy and national security ahead of a speech on the subject Obama is to give on Wednesday at the U.S. Military Academy in West Point, New York.
"The president's decision to set an arbitrary date for the full withdrawal of U.S. troops in Afghanistan is a monumental mistake and a triumph of politics over strategy," Republican Senators John McCain and Lindsey Graham said in a statement.
A senior Obama administration official bristled at the notion that the United States would be leaving Afghan forces to do battle against the Taliban alone.
"We never signed up to be the permanent security force in Afghanistan to fight against the Taliban," the official, speaking on condition of anonymity, told reporters.
The United States now has about 32,000 troops in Afghanistan. U.S. military leaders had pushed for a force of at least around 10,000, saying it was the minimum required.
Remaining U.S. and NATO forces will advise Afghan forces, focusing on functions such as budgeting, logistics, and support for security institutions.
NATO countries have helped build Afghanistan's military and other forces from scratch since 2001. While Afghan forces have grown more independent, they lack key skills such as intelligence collection and air power.
As part of the post-2014 force, a small number of U.S. soldiers is expected to conduct counterterrorism operations against al Qaeda and other hardline militants, located mainly in remote areas along Afghanistan's eastern border with Pakistan. (Additional reporting by Phil Stewart, Missy Ryan, David Alexander, Patricia Zengerle, Jeff Mason and Roberta Rampton; Editing by David Storey and Jonathan Oatis)