The US has handed over to Afghanistan the only prison still under American control, resolving an issue that has strained ties between the countries.
A transfer ceremony took place at the prison, which was renamed the Afghan National Detention Facility at Parwan.
It had been delayed while the two sides finalised a deal over the fate of prisoners considered dangerous.
The handover came as US Secretary of State John Kerry arrived in Afghanistan on an unannounced visit.
He is to meet Afghan President Hamid Karzai to discuss a series of issues, such as the transfer of security to Afghan forces.
Bagram jail has a chequered reputation, having been at the centre of a number of prisoner abuse allegations.
It was once located in Bagram air base, one of the largest military bases for Nato-led forces in Afghanistan, about 40km (25 miles) north of the capital, Kabul. The new Parwan facility was constructed a few miles away and populated with inmates in 2010.
"The transfer of the detention facility is an important part of the overall transition of security lead to Afghan National Security Forces. This ceremony highlights an increasingly confident, capable and sovereign Afghanistan," said Gen Dunford, the commander of US forces in Afghanistan.
Last March the US agreed to hand over responsibility for the majority of the detainees, thought to number more than 3,000, and actually held a transfer ceremony last September.
But the full transfer was held up over a disagreement about the fate of certain inmates who the US feared could be released if the prison was handed over.
US forces remained at the prison and continued to guard about 50 inmates as well as those taken into custody since the initial transfer deal, AFP news agency reports.
Earlier this month a ceremony transferring the final prisoners to Afghan custody was called off at the last minute because President Karzai reportedly rejected part of the deal.
The Pentagon said over the weekend that an agreement had been reached. Reports say this ensures that prisoners of concern will not be released from the detention centre without a full review.
The prison been described as "Afghanistan's Guantanamo".
In January 2012, Afghan investigators accused the US Army of abusing detainees at Bagram. The investigators said prisoners had reported being tortured, held without evidence and subjected to humiliating body searches.
Nato and the US have rejected allegations of abuse as untrue and pointed to the fact that they have given the Afghan Human Rights commission access to check them independently.
In February 2012, US soldiers unwittingly burned Korans confiscated from prisoners at Bagram, leading to days of protests and targeted killings across Afghanistan.
A US investigation said there was no malicious intent to insult Islam.
But the future of the US-run prison had become a thorn in the side of US-Afghan relations ahead of the planned withdrawal of these forces in 2014.
Many Nato forces have already begun withdrawing troops from Afghanistan ahead of the complete transfer of power to Afghan security forces.