Secretary of State John F. Kerry meets with Afghan President Hamid Karzai at the presidential palace in Kabul. They reached a compromise on some Taliban prisoners but did not provide details. (Jason Reed/AP)

Secretary of State John F. Kerry and Afghan President Hamid Karzai smoothed over one of the worst patches in their countries’ difficult relationship with a compromise Monday that settles — for now — a bitter dispute over the fate of Taliban prisoners deemed a threat to U.S. forces.

The transfer of the Parwan detention center to Afghan control is symbolic of the larger tension as U.S. forces depart Afghanistan after a dozen years, leaving behind a fragile democracy with an untested ability to defend itself or safeguard the political and economic gains underwritten by billions in U.S. spending.

With a self-imposed deadline of December 2014 for American combat forces to leave Afghanistan, the Obama administration is trying to shape a less volatile relationship with Karzai while insisting on a clean election next year to replace him. Kerry put his long friendship with the Afghan president on full display Monday, praising him for his courage and endurance, while Karzai repeatedly thanked Kerry and other American officials for sticking by him.

“You, I think, stand on the brink of a remarkable legacy for having brought Afghanistan through an amazingly difficult time,” Kerry told Karzai while in Kabul on his second foreign trip as secretary of state. “There are still difficulties ahead; there are still challenges.”

The two nations are trying to sort out difficult issues that often pit U.S. goals for the security of American forces and interests against Karzai’s keen and increasingly outspoken defense of Afghanistan’s national sovereignty.

The largest of these issues remains unresolved: whether any U.S. troops who stay in the country for training and counterterrorism operations after 2014 would be immune from prosecution under Afghan law. U.S. officials say that protection is essential for a long-term joint security agreement, but it will be a hard sell to Afghans. The same dispute sank a hoped-for security agreement that would have left a training and stability force in Iraq.

U.S. officials say it would be foolish to think that the process of separating U.S. and Afghan control will be smooth. Some of Karzai’s recent statements are Exhibit A.

New Defense Secretary Chuck Hagel canceled a news conference with Karzai this month after Karzai was widely quoted accusing the Obama administration of colluding with the Taliban insurgency that has tried to overthrow him for a decade. The remark angered U.S. officials and many in Congress.

Kerry and others have assured the Afghan president that direct talks between the United States and the Taliban remain on hold, and Karzai said Monday that his complaint had been misunderstood.

He attempted to put into context some of his recent inflammatory remarks, particularly his accusation that U.S. troops had tortured civilians outside Kabul.

“When I say something to this effect, it’s not to offend our allies but to correct the offense,” Karzai said. “I am the president of this country. It’s my job to provide all the protection I can” for Afghans.

Kerry — who knows the unpredictable Afghan leader well and, while serving in the Senate, was a frequent intermediary for President Obama before becoming secretary of state last month — seemed ready to let the matter slide.

“I am confident that the president absolutely does not believe the United States has any interest, except to see the Taliban come to the table to make peace” while helping Afghanistan defend itself, Kerry said after lengthy meetings at Karzai’s graceful stone palace.

Relations between Karzai and the White House have been fraught for years, alternating between periods of close cooperation and periods of U.S. frustration and Afghan anger.

Karzai’s rhetoric was noticeably muted at Monday’s news conference with Kerry, where he focused largely on his appreciation of the U.S. contribution in Afghanistan. With the handover of the Parwan detention center and the U.S. announcement last week that troops would be gradually removed from Wardak province, it appears that Karzai’s most vehement demands have had an impact.

Kerry and Karzai praised a prison agreement that both said ensures that Afghan sovereignty over its own affairs is respected. Officials withheld many details about the deal, including whether the United States will keep its veto power over the release of 30 to 40 detainees referred to as “enduring security threats,” who American officials say pose a serious danger to U.S. and Afghan troops.

“It is important for the people of Afghanistan,” Karzai said of the prison, known as Bagram to Afghans, which has been a symbol of sometimes heavy-handed U.S. military control. “It is very closely linked to our sovereignty, and our sovereignty has to be respected.”

Kerry said Karzai “agrees that there are certain people there who shouldn’t be out creating problems,” and Karzai described a process for reviewing cases about which the United States and Afghanistan disagree. He was vague about what would happen if that review did not resolve the differences.

It is also unclear whether the U.S. mechanism of “administrative detention,” under which some detainees remain in custody without trial, will be continued under Afghan control. Kerry said the announcement of the transfer means the United States no longer holds prisoners in Afghanistan.

At the end of last year, about 3,800 detainees were at Parwan, about 700 of whom were under American control. The rate of release was significantly higher in cases adjudicated by Afghans.

Kerry’s trip also took him to Iraq. In both war-scarred countries, the secretary pushed a message of U.S. support while making clear that Washington won’t call all the shots.

As a senator, Kerry traveled to Afghanistan during similarly tense moments. In 2009, he went to Kabul to urge Karzai to hold a runoff election after a victory that many saw as tainted by fraud. In 2010, Kerry accused Karzai of ignoring corruption in his government, warning the Afghan president that his poor leadership could embitter both American lawmakers and the families of U.S. troops.

Kerry and Karzai said they had helpful discussions about the prospect of reconciliation with the Taliban, but they gave few specifics. The issue is complicated by Karzai’s demand that the Persian Gulf nation of Qatar sign a memorandum of understanding that would allow for the opening of a Taliban office in Doha.

Officials in Qatar have refused to sign such an agreement, which would outline the purpose of the Taliban office and codify the Afghan government’s role in peace talks. Meanwhile, Taliban representatives are often in the city, holding unofficial meetings that U.S. officials said are sometimes constructive for wider peace talks.

In an apparent breakthrough, though, Karzai announced last week that he will soon travel to Qatar to discuss future Taliban negotiations. The president said Monday that his government has had recent “individual contacts” with the Taliban, which he called a step toward reconciliation.

Obama supports the opening of the political office, which would give an address for talks with the U.S.-backed government in Kabul without conferring wider legal rights on the Taliban.