Image caption Although a handover ceremony took place last September, US forces remained at the prison pending a deal over the fate of certain prisoners

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 Bagram jail, now renamed the Afghan National Detention Facility at Parwan.

The handover came as US Secretary of State John Kerry made an unannounced visit for talks with President Karzai.

Mr Kerry told a press conference they were both "on the same page" regarding peace talks with the Taliban.

President Hamid Karzai recently angered Washington by accusing the US and the Taliban of colluding to prolong the conflict.

But Mr Kerry told reporters: "I am confident (Mr Karzai) does not believe the US has any interest except to see the Taliban come to the table to make peace.''

Analysis Many analysts believe Hamid Karzai was using the issue of Bagram and Wardak - where he recently ordered US troops to leave the province - as a tool to prove his mettle as a national figure committed to Afghanistan's national sovereignty. But President Karzai is also calculating that he may be able to use some of the Taliban detainees as part of any future national reconciliation process with the Taliban. Many of these fighters are from his home province Kandahar and he has said in the past that he will release "innocents" once the prison is handed over.

"So we're on the same page. I don't think there is any disagreement between us.''

Referring to the handover of Bagram prison, Mr Karzai said: "Today was a very good day.''

'Afghanistan's Guantanamo'

The handover had been delayed while the two sides finalised a deal over the fate of prisoners considered dangerous.

Bagram 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.

Bagram timeline 1979-88: Key base for troops and supplies during Soviet occupation

Key base for troops and supplies during Soviet occupation 2001: Air base taken over by coalition forces following fall of Taliban regime, also becomes the main US detention facility in Afghanistan

Air base taken over by coalition forces following fall of Taliban regime, also becomes the main US detention facility in Afghanistan July 2005: Escape by four "dangerous enemy combatants", the first by any prisoners

Escape by four "dangerous enemy combatants", the first by any prisoners February 2007: Suicide attack outside base during visit by US Vice President Dick Cheney kills more than 20 people

Suicide attack outside base during visit by US Vice President Dick Cheney kills more than 20 people 2010: Thousands of prisoners moved to Parwan Detention Centre, built outside base with extra capacity to replace old prison

Thousands of prisoners moved to Parwan Detention Centre, built outside base with extra capacity to replace old prison April 2010: Allegations surface of prisoner abuse at a hidden facility at Bagram

Allegations surface of prisoner abuse at a hidden facility at Bagram February 2012: Violent protests erupt after US troops inadvertently burn copies of the Koran at Bagram - at least 30 are killed

Violent protests erupt after US troops inadvertently burn copies of the Koran at Bagram - at least 30 are killed March 2012: Agreement to hand over jail to Afghan control

Agreement to hand over jail to Afghan control September 2012: Handover ceremony held but US forces remain to guard a core of "dangerous" prisoners

Handover ceremony held but US forces remain to guard a core of "dangerous" prisoners March 2013: Final handover ceremony on 9 March cancelled by US over concerns over fate of those prisoners. Transfer of jail to Afghan control eventually held on 25 March 'Rebranding' US detention in Afghanistan

But the full transfer was held up over a disagreement about the fate of inmates whom the US feared could be released if the prison was handed over.

A senior official at the facility told reporters: " Since March 2012, the Americans have handed over to us 4,000 prisoners. We have freed 1,350 of them. The Americans have given us 26 more prisoners today, and they still have more prisoners which they will hand over to us soon.''

According to the latest deal Afghanistan will not release "dangerous" inmates without review, and about 50 foreign inmates will remain in US hands.

If the US captures fighters, they must be turned over to the Afghan authorities within 96 hours.

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.

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.