Updated

A last-minute agreement has been reached on how to handle the transfer of U.S. detention facilities in Afghanistan to the Kabul government, Afghan and Western officials said Friday.

The issue -- along with night raids by international forces in Afghan villages -- has threatened to derail a long-term partnership between the two countries. They are in negotiations to formalize a role for U.S. forces after NATO's scheduled transfer of security responsibility to the Afghan government by the end of 2014.

Afghan President Hamid Karzai set Friday as the deadline for handover of control of the Parwan detention facility, a U.S.-run prison that holds about 3,000 detainees, most of them Afghan citizens.

The deal comes as relations between the U.S. and Afghanistan have become more tense in recent weeks following the burning of Korans and other religious materials at Bagram military base near the capital Kabul, sparking riots and attacks that killed some 30 people.

The U.S. has apologized and said the Korans came from the Parwan detention center and were taken out because they had extremist messages written in them, but that they should not have been sent to be burned. Karzai said soon after the Koran burnings became public that these types of incidents would not occur if the Afghans were in charge of the detention facility.

Under the agreement, a three-star Afghan commander will be formally in charge of Parwan, and a joint U.S.-Afghan commission will be involved in running the prison, said a Western official familiar with the deal.

The U.S. will oversee the transfer process and be consulted on decisions, said the official, speaking on condition of anonymity because the agreement had not yet been signed. It's still unclear how long it will take to transfer operational control to the Afghans.

Earlier, Afghan officials had indicated that some in the government in Kabul were ready to accept an American proposal to delay the transfer for six months in the framework of an agreement, but it was not clear if Karzai would go along with that.

Parwan is the largest of the facilities, located next to Bagram. The U.S. previously handed over responsibility for a few hundred detainees there and said the Afghan government was not ready to take over running the entire detention center.

Afghan Defense Minister Gen. Abdul Rahim Wardak and U.S. Gen. John Allen, the commander of NATO and U.S. forces in Afghanistan, will sign a memorandum later Friday, the Afghan Foreign Ministry said in a statement.

The ministry did not say what the memorandum covered, but a Western official confirmed that it was about the transfer of detention facilities. The official said the exact wording of the agreement was still being discussed, but the major issues have been decided.

The official declined to give other details and spoke anonymously because the document was still being finalized.

An Afghan official confirmed separately that the agreement was about detention issues but declined to give more details. The official spoke anonymously to discuss the deal before it was made public.

U.S. and Afghan officials have said that they want a strategic partnership agreement signed by the time a NATO summit convenes in Chicago in May, but talks have stalled in recent months, partly because of the disagreement over the detention facilities.

Several other key issues remain unresolved.

Karzai has demanded an end to night raids in Afghan villages by coalition forces. The raids target insurgents, but Karzai has said civilians are too often rounded up or killed when raids turn violent. He insists that if there are night raids, Afghan troops should conduct them alone.

Afghan and U.S. officials said previously that both the detention and night raid issues might be handled separately from the partnership agreement, in order to push a deal through.

President Obama and Karzai discussed the stalled security pact talks in a video conference on Thursday, the White House said.

White House press secretary Jay Carney said the two leaders noted progress toward completing an agreement "that reinforces Afghan sovereignty while addressing the practical requirements of transition."

The U.S.-Afghan pact is expected to provide for several thousand U.S. troops to stay and train Afghan forces and help with counterterrorism operations. It would outline the legal status of those forces, their operating rules and where they would be based.

The agreement is also seen as a means of assuring the Afghan people that the U.S. does not plan to abandon their country, even as it withdraws its combat forces.

U.S. Embassy spokesman Gavin Sundwall declined to comment on Friday's agreement.

The debate about the agreement has also caused rifts within Karzai's inner circle. Afghan officials have said that National Security Adviser Rangin Dadfar Spanta tried to resign more than two months ago after disagreements erupted between him and Karzai over the strategic partnership document. Spanta argued for more flexibility from his government.