During the transition, custody of the American-held prisoners would gradually be shifted to Afghan authority, with the first 500 prisoners to be transferred within 45 days, according to American military and diplomatic officials who spoke on the condition of anonymity as a matter of policy.

The move to speed up the transfer is a major concession to the Afghans, but the veto power, which would last as long as American troops are in Afghanistan, addresses American worries that Taliban fighters would be released prematurely and return to the battlefield. In order for a detainee to be released, both General Allen and Mr. Wardak would have to agree, American officials said.

They said the United States would also continue to monitor treatment of the prisoners in an attempt to prevent human rights abuses prevalent in many Afghan prisons.

The compromise was part of an effort to win support for a strategic partnership agreement that would provide for continued American military support even after the end of the NATO mission in 2014. The agreement is considered crucial because the Americans hope to ensure that the Afghan government can hold out against the Taliban and that Al Qaeda cannot re-establish a stronghold.

The American ambassador, Ryan C. Crocker, suggested that both sides had compromised to reach the deal. “A good agreement represents a balance of concessions on both sides,” he said after the formal signing by General Allen and Mr. Wardak. “I won’t say they were the side that gave up the most, but the side that lost the most was the Taliban.”