The disagreement now threatens to complicate an even larger issue: the two countries began negotiations last week on a status of forces agreement that would govern any American military presence remaining in Afghanistan after 2014.

In the statement by Mr. Karzai’s office on Monday, he was quoted as saying the Americans were in “serious breach” of the prison transfer agreement and ordered Afghan officials to take “urgent measures to ensure a full Afghanization of the prison affairs and a complete transfer of its authority.”

American officials, however, say it is not so simple. One official, speaking on the condition of anonymity because of the issue’s sensitivity, said: “It’s an issue of sovereignty for the government of Afghanistan, and to General Allen it’s a matter of security for the coalition troops. You can’t just bring these guys in and let them go.” Gen. John R. Allen is the departing commander of American and NATO troops here.

The American military’s statement maintained that the agreement “contains reciprocal commitments to provide for the security of Afghan citizens, the A.N.S.F. and coalition forces by keeping captured enemy combatants from returning to the battlefield.” It added that the military was confident of working out a solution with Afghanistan.

Mr. Faizi said there would not immediately be a change in control over the Bagram facility, despite the Afghan president’s strongly worded statement. He said Afghan officials would hold discussions with American officials and report back to Mr. Karzai in the next few days about how to carry out his order. After that, Mr. Karzai would announce his government’s next steps.

Bagram Prison, which can hold more than 3,000 detainees, lies within Bagram Air Base and is surrounded by American checkpoints, and is heavily staffed by American guards.

Rights advocates welcomed Mr. Karzai’s move. Tina Foster, the executive director of the International Justice Network, who represents some Bagram detainees, met recently with Afghan officials to try to visit her clients in the prison. The Afghans approved the visits, but the United States military blocked them, she said.

“When we met with Karzai’s staff it was clear that the Afghans are tired of being treated like servants in their own country,” Ms. Foster said. “Symbolic gestures are not going to cut it anymore. They want the keys to the prison, and the ability to determine the fates of the prisoners held there.”