“It is with deep sadness that we learn of the passing away of Marshal Mohammad Qasim Fahim,” Mr. Karzai’s office said, declaring three days of national mourning.

Though Mr. Fahim was at the center of Mr. Karzai’s government, the two shared a tumultuous history. In the mid-1990s, when Mr. Fahim was in charge of the Afghan intelligence service after the fall of the Soviet-backed regime, he ordered the arrest of Mr. Karzai, then the deputy foreign minister, on suspicion of spying for a rival faction within the government. The future president managed to escape when a rocket hit the prison where he was being held.

Nearly a decade later, Mr. Fahim backed Mr. Karzai’s bid to lead Afghanistan after the Taliban’s fall, and in exchange was named defense minister. But within months, as Mr. Karzai began moving to sideline Mr. Fahim, American officials picked up intelligence that the defense minister was considering an attempt to assassinate the president, according to current and former Afghan and American officials with firsthand knowledge of the episode. To head off the threat, the Americans quickly replaced the Fahim loyalists who were guarding Mr. Karzai with United States Army Rangers.

But they did not replace Mr. Fahim, concluding that the young Afghan government was too weak to risk a confrontation with one of Afghanistan’s most powerful warlords. American and Afghan officials instead settled for vague statements that suggested Taliban threats had led to the bodyguard swap.

Mr. Fahim left the government after Mr. Karzai won Afghanistan’s first presidential election in 2004. But he returned as one of Afghanistan’s two vice presidents when Mr. Karzai was re-elected in 2009, and immediately helped broker an end to a postelection crisis involving another northern Tajik who also ran for president, Abdullah Abdullah.