Afghanistan on Monday took full control of Bagram military prison from the United States, healing one running sore in their testy relationship as US-led forces wind down more than a decade of war.
President Hamid Karzai had made the fate of the detention centre north of Kabul part of his ill-tempered push to regain sovereignty over key matters from the Americans, ahead of next year's pullout of foreign combat troops.
The United States was long concerned that a total handover to Afghanistan's weak and corruption-prone security forces would allow suspected Taliban and Al-Qaeda militants housed at Bagram to return to the battlefield.
But US Defense Secretary Chuck Hagel clinched an agreement with Karzai in a telephone call on Saturday, the Pentagon said, and the handover ceremony took place on Monday.
"This ceremony highlights an increasingly confident, capable and sovereign Afghanistan," General Joseph Dunford, the commander of the international coalition in Afghanistan, said in a statement.
Bagram was due to be turned over to Afghan forces on March 9, but the transfer was postponed at the last minute after Karzai indicated that "innocent" prisoners held there would be released.
In September the United States gave Afghan authorities control over more than 3,000 detainees at Bagram, once dubbed the Guantanamo Bay of Afghanistan because some inmates are detained without trial or knowledge of any charges.
But the Americans continued to guard 50 foreigners not covered by the agreement, as well as hundreds of Afghans arrested since a transfer deal was first signed in March 2012.
Their extended control sparked angry outbursts from Karzai and a warning from Afghanistan's top Islamic body that the US military was coming to be seen as an "occupation" force as it battles a long-running Taliban insurgency.
"US control of Bagram was a rallying cry for the Taliban and an important issue for much of the Afghan public," Kate Clark of the Afghanistan Analysts' Network said.
"This looks like a victory for Karzai as he has got what he wanted. Bagram was a huge stumbling block before they get onto a long list of other issues to sort out -- and time is of the essence."
Karzai, who leaves office next year, has also long been chafing at the activities of US special forces and at civilian casualties. He recently triggered outrage by accusing the US of colluding with the Taliban to justify the presence of foreign troops.
Last week he won a limited agreement for Afghan forces to take charge of one district of Wardak province, a key front of the insurgency pitting insurgents against the US-led International Security Assistance Force (ISAF).
The agreement made no mention of Karzai's demand for US special forces to withdraw entirely from Wardak, and ISAF said that it was "business as usual" for its troops in the rest of the flashpoint province.
Afghan army and police are gradually taking on responsibility for battling the Taliban insurgency as most of the 100,000 foreign troops prepare to exit by the end of next year under a timetable laid down by US President Barack Obama.
Karzai is due to step down at elections next year, 13 years after he came to power with US backing when the hardline Taliban regime was ousted in the aftermath of the September 11 attacks of 2001.
The military and political calendars are lending added urgency to the search for a negotiated settlement to resolve Afghanistan's decades of conflict.
Karzai plans to visit Qatar "within weeks" to discuss the proposed opening of a Taliban office in the Gulf emirate as a prelude to possible peace talks, the Afghan foreign ministry said Sunday.
Until earlier this year, Karzai rejected the idea of a Taliban office in Qatar because of fears that his government would be frozen out of any deal between the US and the militants.
The Taliban have refused to negotiate directly with Karzai, and the foreign ministry stressed that it would only start negotiations if the militants "break all relations with Al-Qaeda and give up terrorism".