American officials have agreed to gradually transfer control of most of their detainees here to the Afghan government over the next six months, U.S. and Afghan officials announced Friday, possibly eliminating an irritant in Washington’s strained relationship with Kabul as the U.S. military mission starts winding down.

The step appeared to remove a key obstacle to a broader deal stipulating the terms of a U.S. military presence in Afghanistan beyond 2014 and other aspects of a long-term bilateral relationship.

The agreement marked a significant concession by the United States, which has been reluctant to hand over responsibility for its detention operations to a government with a poor human rights record and a weak judicial system.

Still, it leaves the U.S. military with considerable authority. American soldiers may continue to detain individuals whom commanders deem particularly dangerous even after the six-month transition period.

The United States will also retain veto power if it disagrees with the Afghan government’s decision to release certain inmates, according to the agreement, which was signed by the top U.S. commander in Afghanistan, Gen. John Allen, and Afghan Defense Minister Abdul Rahim Wardak.

U.S. officials appeared delighted to have reached a deal just weeks after the burning of Korans by U.S. soldiers in Afghanistan sparked a wave of violent protests and brought the relationship between the two countries to a perilous low.

“This is a message of peace and stability for the region,” Allen said in a statement. “It is also a message for the Taliban: Your options are getting fewer and fewer. The time is now to join the peace process and be part of the future of Afghanistan.”

Under the agreement, the U.S. military will transfer legal responsibility for batches of detainees over the next six months, until nearly all of the more than 3,000 inmates are nominally in Afghan custody.

Some human rights activists expressed concern that the Afghan government has not created a legal framework to assume responsibility for the detention system. John Sifton, Asia director for Human Rights Watch, said Afghanistan should immediately pass a law that “explains the foundation of this system of administrative detention, when it expires and how it will be renewed.”

He noted that among the first acts of an Iraqi sovereign government was the declaration of a state of emergency, which allowed for the creation of a detention system compatible with international law.

The Afghan system, Sifton said, “is lawless because there is literally no law.”

The agreement does not address the cases of the roughly 50 non-Afghan prisoners held by U.S. forces here.

An Afghan general will be appointed as the commander of the Parwan detention facility, a state-of-the-art complex designed to the standards of a U.S. federal prison. Parwan is next to Bagram air base, one of the largest U.S. military installations in the country.

U.S. military officials will work alongside Afghan corrections officials as advisers for a time beyond the transition period and will seek to ensure that detainees are not mistreated, U.S. officials said. Because the Afghan government is not expected to be able to run the highly sophisticated facility without substantial help after the transition period, the U.S. military will build more rudimentary cell blocks in Bagram and at a prison near Kabul, the officials said.

Afghan officials have long seen the U.S. military detention system in Afghanistan as an affront to their sovereignty. Detainees at Parwan are held without formal charges, and until now the Afghan government has been largely powerless to weigh in on individual cases. Afghan authorities have also voiced unease about the presence of foreign detainees who were captured in third countries and transferred to Afghanistan.

The non-Afghan inmates will remain in U.S. custody for now, but Friday’s agreement is likely to add a sense of urgency to finding a resolution for those cases, Obama administration and foreign officials said.

Some in the Obama administration advocate the gradual repatriation of most, if not all, of the non-Afghan detainees because there are few options for holding them indefinitely.

U.S. officials say they believe the Afghan authorities will have no interest in continuing to detain foreigners after a full American drawdown. The administration has repeatedly said it will not transfer anyone else to the military detention center at Guantanamo Bay, Cuba. And Republicans and some Democrats have strongly opposed bringing such prisoners to the United States for continued detention or trial, in civilian courts or military commissions.

That leaves the administration little choice but to negotiate their transfer to their home countries and secure commitments that they will continue to be monitored — closely, in some cases.

Pakistanis make up the largest segment of the non-Afghan detainees, followed by Central Asians and Arabs of various nationalities, according to administration and foreign officials. Among them are two Yemenis and a Tunisian who unsuccessfully attempted to secure their release through appeals to the federal courts.

The U.S. Court of Appeals in Washington said detainees in Afghanistan do not enjoy the right to challenge their detention under the writ of habeas corpus, as detainees at Guantanamo do.

The deal was signed on the second deadline that Afghan President Hamid Karzai had set for the United States to relinquish full control of its prisons in Afghanistan.

“We are making sure this transition is not abrupt, but a path marked by benchmarks and steps that build Afghan capacity and a partnership with Afghan security forces,” U.S. Ambassador Ryan C. Crocker said in a statement.

Afghan and U.S. officials remain divided over the second major roadblock to signing a long-term bilateral agreement: the night raids carried out by U.S. Special Operations Forces that the Afghan government has decried as a violation of its sovereignty.

“Night raids is still some time away from an agreement,” a senior Obama administration official said, speaking on the condition of anonymity to discuss a sensitive issue.

The official said the U.S. goal is to complete the agreement before a NATO summit in May, when the alliance hopes to make decisions about plans to withdraw from Afghanistan.

Finn reported from Washington. Staff writer Karen DeYoung and staff researcher Julie Tate in Washington contributed to this report.