The United States will turn full control of the Parwan prison over to the Afghan government on Monday, the Pentagon said, settling a dispute that had threatened negotiations over a long-term security accord that would allow U.S. troops to remain in Afghanistan after 2014.

Defense Secretary Chuck Hagel confirmed the prison agreement Saturday in a telephone conversation with Afghan President Hamid Karzai, according to Pentagon spokesman George Little.

The scheduled turnover of the facility — located at the sprawling U.S. Bagram base in Parwan province northwest of Kabul — was canceled two weeks ago after Karzai refused to guarantee he would not set free about three dozen Taliban prisoners that the United States considers particularly dangerous to U.S. forces and likely to return to the battlefield.

Hagel welcomed 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,” Little said.

The text of the detainee agreement was not released. A senior defense official said that Afghanistan will have full sovereign control of the facility, but that “an Afghan-led process with an opportunity for U.S. input will determine the disposition of detainees.”

The United States, the official said, has received “private assurances from Afghan officials that the most dangerous of the detainees will not be released.”

When Hagel, on his first overseas trip as defense secretary, visited Kabul the day after the turnover was canceled, Karzai publicly accused the United States of breaking an agreement on the facility and disrespecting Afghan sovereignty.

He also charged U.S. forces with harassing and torturing Afghan civilians in the province of Wardak, west of Kabul, and accused the United States of colluding with the Taliban to prolong the war.

The dispute disrupted negotiations over a larger bilateral security agreement that will set the terms for a continued U.S. military presence in Afghanistan after the final withdrawal of combat troops in December 2014. Karzai equated Afghan sovereignty over the prison to the United States’ insistence that the U.S. troops be given immunity from Afghan legal jurisdiction as part of a post-2014 security accord.

The Obama administration considers completion of the accord by this summer crucial to timely planning of ongoing counterterrorism operations and long-term military and civilian assistance to Afghanistan.

Afghan officials attributed Karzai’s public excoriation of the United States to domestic political constraints as Afghanistan heads toward elections next year and Karzai seeks to establish his legacy as an independent leader. U.S. officials expressed private outrage but said little.

The Wardak issue was settled last week, when Karzai and Gen. Joseph F. Dunford Jr., the top U.S. commander in Afghanistan, announced that the American troops would gradually pull out of the province, along with U.S.-trained Afghan local police units. The Afghan Defense Ministry said they would be replaced with Afghan army units but set no timeline.

The prison issue was more difficult. Afghan officials said Karzai would not be able to build public support for the bilateral security agreement, including U.S. troop immunity, unless he prevailed on the transfer.

Last year, the United States turned thousands of battlefield prisoners over to Afghan custody at Parwan prison. Although many have been tried and convicted under Afghan penal code, about 100 have been released each month for lack of evidence.

By December, only about 700 prisoners — including those newly detained on the battlefield — remained under American control. Among them are 30 to 40, some of them held for years, that the U.S. military considers “enduring security threats.” Evidence to convict them is considered too sensitive to be turned over to the Afghans or considered otherwise unavailable.

After a verbal agreement was reached over the high-threat prisoners during Karzai’s visit to Washington in January, the two sides were unable to come up with written language acceptable to Karzai and the scheduled March 8 handover was canceled.

On March 16, Hagel and Karzai spoke by telephone and agreed to what the Pentagon called “intensive work” to try and reach a new prison agreement within a week.

Afghan officials had argued that Afghan law contains no provision for the kind of “administrative detention” that would allow it to retain prisoners without trial. It was unclear how that issue was resolved.

Under the agreement finalized Saturday, all prisoners will be transferred to Afghan control, although the senior defense official said that some U.S. military personnel will remain at the prison in an advisory capacity.

“It wouldn’t be surprising if some detainees are released soon after transfer,” the official said. “But it would be surprising and unfortunate if dangerous detainees were released — particularly after assurances have been given that they won’t be let go.”