KABUL — Afghan President Hamid Karzai lashed out at the United States in strikingly acerbic terms Sunday, implying that the American military was stoking violence in collusion with the Taliban to justify a prolonged presence here and charging that foreign troops were harassing Afghan university students.

The remarks painted an embarrassing picture of discord that marred Defense Secretary Chuck Hagel’s first foreign trip as Pentagon chief and plunged the tenuous allies into crisis mode at a time when the United States is struggling to wind down the unpopular war in a dignified manner.

Afghan and American officials provided differing accounts about why a joint news conference scheduled for Sunday night that had been expected to be the centerpiece of Hagel’s trip was canceled. U.S. officials said they scrapped the event in consultation with the Afghan government because of an unspecified security threat. Karzai’s spokesman, Aimal Faizi, rejected the notion that the palace would have been a dangerous place to hold a news conference.

“From our side, we saw no threat,” he said.

In stark contrast to Kabul visits by other U.S. defense secretaries, Hagel completed his trip without making upbeat public pronouncements about the state of the U.S. mission.

Later Sunday night, Hagel met with Karzai in private. After the meeting, the secretary told reporters he was hopeful the two countries could overcome the latest crisis.

“I know these are difficult issues for President Karzai and the Afghan people,” Hagel said.

In a televised speech, Karzai said two fatal bombings carried out Saturday, including one outside the Ministry of Defense, should not be interpreted as a Taliban show of force aimed at undermining the U.S. military, which is considering keeping a small force in Afghanistan after its wartime mandate expires at the end of 2014.

“In reality, the bombs that went off yesterday under the name of the Taliban were a service to the foreigners,” Karzai said, casting doubt on the claim of responsibility made by the Taliban, which said the attacks were carried out to mar Hagel’s visit.

Karzai said the blasts helped Americans justify a prolonged presence in Afghanistan.

“We have been down this road before too many times,” he said.

U.S. officials struggled to make sense of the tone and timing of Karzai’s remarks, but the top U.S. commander in Afghanistan pushed back against the notion that the U.S. military could be complicit in attacks.

“We have fought too hard over the past 12 years,” Gen. Joseph Dunford told reporters in Kabul on Sunday. “We have shed too much blood over the past 12 years; we have done too much to help the Afghan security forces grow over the past 12 years to think that violence or instability would be to our advantage.”

Dunford suggested Karzai’s anti-American rhetoric could be political gamesmanship.

A senior Afghan official recently said Karzai has opted to take the Americans to task in public because he thinks Western officials don’t take him seriously or listen to him during private meetings. The official spoke on condition of anonymity to speak candidly about Karzai’s mind-set.

Hours after the speech, Karzai’s office issued an executive order threatening to resort to “legal action” if international forces don’t stop harassing, annoying and detaining Afghan students.

Karzai’s anger has been building in recent weeks. Two weeks ago, he imposed a two-week deadline for the expulsion of U.S. Special Operations forces from Wardak, a crucial eastern province, accusing the forces of murder and other abuses. The U.S. military said an investigation into the claims turned up no evidence to substantiate them and disregarded the ultimatum, saying U.S. forces would leave the area when it is prudent to do so.

Dunford said Karzai’s rhetoric should not create the impression that trust between the two nations is irreparably broken. In fact, he argued, senior U.S. and Afghan security officials see eye to eye on most things.