KABUL, Afghanistan — On his last planned trip to Afghanistan as defense secretary, Ashton B. Carter on Friday said that the United States remained committed to the country even as the war here has worsened.

The unannounced visit by Mr. Carter was part of his “round-the-world trip to thank deployed U.S. troops for their service over the holidays” as well as meet with allies, the Pentagon said in a statement.

In Afghanistan, Mr. Carter’s remarks were seen as an attempt to quell concerns over how the transition in Washington could affect the American presence here at a time when the Afghan forces, which have taken heavy losses, have shown the need for sustained help.

Even with the Taliban threatening major cities and Islamic State affiliates gaining a foothold in Afghanistan, the war did not feature much as an issue during the United States presidential campaign. And with his security team still forming, President-elect Donald J. Trump has disclosed little about how he will tackle a conflict that remains a bloody stalemate even as the war enters its 16th year.