Bagram prison will be fully transferred from US military control to the Afghan government on Monday after an agreement between the two countries was finalised Saturday, according to the US Department of Defence.

The Pentagon said that the deal, thrashed out over the past week between Kabul and Washington, was verbally confirmed in a telephone call between newly-appointed US Defence Secretary Chuck Hagel and Afghan President Hamid Karzai on Saturday.

It is understood that they reached a compromise over the misgivings of both parties including a reassurance from Karzai that prisoners deemed to be the most dangerous by the US would not be released.

“The secretary [Hagel] welcomed President Karzai’s commitment that the transfer will be carried out in a way that ensures the safety of the Afghan people and coalition forces by keeping dangerous individuals detained in a secure and humane manner in accordance with Afghan law,” Pentagon spokesman George Little said Saturday in Washington.

The handover is one of the longest running single issues that has plagued the Afghan-US relationship. It has been one of Karzai’s key demands since 2011 and was a pre-condition for the Afghan government to sign a long-term strategic agreement with the US.

The first Bagram transfer deal was signed in March 2012 with the official handover taking place in September. However, while more than 3000 prisoners were transferred to Afghan custody, the US military retained control of at least 600 prisoners it deemed dangerous.

The matter has agitated relations ever since and undermined Secretary Hagel’s visit to Afghanistan earlier this month when Karzai refused to sign the new transfer agreement the day after Hagel arrived in Kabul.

Bagram prison, located in Parwan province which borders Kabul to the north, is America’s only remaining prison in Afghanistan. Its transfer to the Afghan government is crucial to US plans for a gradual drawdown from the country.

Karzai’s office told TOLOnews Saturday that the meetings with the US had been fruitful this week.

“In the meeting, the US side stressed its commitment to handing over Bagram prison to the Afghan Government and all the legal and technical problems have been addressed so the transition will happen soon,” the presidential deputy spokesman Mohammad Fayiq Wahidi said in a phone interview.