KABUL, Afghanistan — Vice President Mike Pence, making an unannounced trip to Afghanistan, reaffirmed the Trump administration’s support for the country’s beleaguered government and starkly warned neighboring Pakistan to stop harboring militant groups that have long undermined the United States-led coalition’s mission here.

Mr. Pence’s brief visit Thursday, during which he met with senior Afghan leaders and spoke to American troops, was not made public until just before he left the country because of security concerns.

Speaking to Afghan officials at the presidential palace in Kabul, the capital, Mr. Pence said the United States was committed to standing by the government as it fights the Taliban and tries to bring stability, more than 16 years after the war began.