Afghanistan's President Hamid Karzai talks with U.S. Secretary of State John F. Kerry after a news conference in Kabul on Saturday. (MOHAMMAD ISMAIL/REUTERS)

The United States and Afghanistan agreed Saturday on a draft deal that would keep some U.S. forces in Afghanistan past next year, but only if Afghan political and tribal leaders agree to a key U.S. demand that American troops not be subject to Afghan law, Secretary of State John F. Kerry said.

Afghan President Hamid Karzai said the framework security agreement meets his demands regarding counterterrorism operations on Afghan soil and respects Afghan sovereignty. The U.S. demand to retain legal jurisdiction over all remaining U.S. forces will be put before a loya jirga, Karzai said. He plans to convene the Afghan tribal consultation body next month.

“Tonight we reached some sort of agreement,” Karzai said through an interpreter. “The United States will no longer conduct operations by themselves. We have been provided a written guarantee of the safety of the Afghan people. And a clear definition of invasion was provided.

Kerry and Karzai broke an impasse in negotiations during two days of intensive talks in the Afghan capital, as an Oct. 31 deadline approaches for negotiating terms for some U.S. forces to remain in Afghanistan after combat forces depart in 2014.

The two welcomed the agreement but said it hinges on the question of legal jurisdiction.

“The one issue that is outstanding is the issue of jurisdiction,” Kerry said at an evening news conference with Karzai. “We need to say that if the issue of jurisdiction cannot be resolved, unfortunately there cannot be a bilateral security agreement.”

Karzai’s insistence on greater Afghan control over counterterrorism operations — one of the main sticking points leading up to Saturday’s agreement — had been underscored in recent days by a successful U.S. mission this month to detain a senior Pakistani Taliban leader whose group claimed responsibility for the 2010 bombing attempt in New York’s Times Square.

Latif Mehsud, a senior deputy to Pakistani Taliban leader Hakimullah Mehsud, was captured Oct. 5, the State Department confirmed Friday. Latif Mehsud was captured while traveling in Afghanistan’s Logar province, Afghan officials said. The operation angered Afghan intelligence and security officials, who said Mehsud had been in their custody when he was forcibly removed by U.S. troops, and it prompted Kerry to travel to Kabul to reassure and lobby Karzai in person.

Kerry said Saturday that he and Karzai had been able to talk through their differences on the issue. Karzai reiterated that the issue was important to Afghans and that the operation went “against Afghanistan’s laws and independence.”

“We should be confident after signing the [agreement] that such incidents will not happen again,” he said.

If a pact is indeed signed, it will come as an enormous relief to many Afghans, including members of the country’s security forces, which are dependent on U.S. funding and supplies.

“Without U.S. help after 2014, there’s no question that the army and police will be in trouble,” said one senior Afghan military official this week.

At the same time, Afghan officials have grown skeptical of such significant security agreements over the past several years. The two countries signed an agreement in 2012, which transferred the control of night raids to Afghan forces. But Karzai has said that the Americans violated that agreement, leading to mistrust.

“We learned our lesson last time,” Aimal Faizi, Karzai’s spokesman, said of the agreement.

There is no guarantee that the outstanding issue of diplomatic immunity will be resolved to the Americans’ satisfaction, but U.S. officials have suggested that Afghan leaders know how much they will lose if a bilateral agreement is not approved.

Still, they know not to assume anything until a deal is signed. Earlier this year, after reaching a tentative agreement on the hand-over of the Bagram military prison, misunderstandings and last-minute objections led to months of delays.

At issue is whether a U.S. force of between 5,000 and 10,000 will remain in Afghanistan after the NATO-led combat operation ends in 2014. Karzai long ago agreed to the schedule for the departure of international forces, which have been both a political irritant in Afghanistan and a key guarantor of Karzai’s political survival.

There are about 87,000 international forces in Afghanistan now, including 52,000 from the United States.

The Obama administration wants a deal by Oct. 31 so that 2014 can be spent organizing the residual force and planning for the end of what will be more than 13 years of combat when the NATO mission concludes.

The agreement provides a legal framework for continued U.S. military operations in Afghanistan, including the leasing of Afghan bases. The Obama administration considers the deal an executive agreement that does not require Senate ratification.

Karzai’s participation in the negotiations is one of his last political acts as president, carried out with an eye to his legacy as the first modern-day elected leader of Afghanistan. He leaves office in April, and nearly 30 candidates have begun electioneering.

Kevin Sieff and Mohammad Sharif contributed to this report.