Afghanistan on Monday took full control of Bagram prison from the United States, healing a running sore in their relationship as US Secretary of State John Kerry arrived unannounced in Kabul.
President Hamid Karzai had made the fate of the detention centre part of his push to regain sovereignty over key matters from the Americans ahead of the scheduled withdrawal of foreign combat troops next year.
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 a final agreement was sealed on Saturday, and a handover ceremony was held at the jail hours before Kerry landed in Kabul on an unannounced visit.
The drawn-out war in Afghanistan is increasingly unpopular in the US, and relations between Kabul and Washington were badly shaken earlier this month when Karzai accused the US of working in concert with Taliban militants.
Reacting to Karzai's shock allegations, a US official travelling with Kerry said the secretary of state was keen to move "beyond this incident" and to "continue to emphasise on how we work together".
"He will make clear that the US will have an enduring commitment in Afghanistan that will last beyond transition and that there will always be bumps on the road," the official told reporters.
Bagram was due to be turned over on March 9, but the transfer was postponed at the last minute after Karzai indicated that "innocent" prisoners would be freed.
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 comments 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," said Kate Clark of the Afghanistan Analysts Network.
"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."
General Joseph Dunford, commander of the international coalition, and Defence Minister Bismillah Mohammadi signed a deal guaranteeing the treatment of detainees and "to protect the people of Afghanistan and coalition forces".
Mohammadi hailed the transfer as "a crucial and positive step towards the security and self-sufficiency of Afghanistan", before 26 detainees -- who were not present at the ceremony -- were released.
Guards prevented reporters from speaking to the freed men.
Karzai has taken an increasingly antagonistic stance towards Washington and has been chafing at the activities of US special forces and at civilian casualties.
Last week he won a limited agreement for Afghan forces to take charge of one district of Wardak province, a key front in the insurgency pitting militants against the US-led International Security Assistance Force.
Afghan troops and police are gradually taking on responsibility for battling the Taliban as most of the 100,000 foreign troops prepare to leave by the end of 2014.
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.
Kerry, on his first trip to Afghanistan since taking up office, arrived in Afghanistan after visiting the Middle East.