The Pentagon announced on Friday that virtually all of the approximately 700 troops in Somalia — most Special Operations troops who have been conducting training and counterterrorism missions — will be leaving by Jan. 15, five days before President-elect Joseph R. Biden Jr. is scheduled to be inaugurated.

Many of the troops will be “repositioned” to nearby Kenya, a Defense Department official said Friday. It was not immediately clear whether other parts of the American presence in Somalia — such as C.I.A. officers, the ambassador and other State Department diplomats who are based at a heavily fortified bunker at the airport in Mogadishu, the Somali capital — will also withdraw from Somali territory along with the military.

The withdrawal from Somalia followed Mr. Trump’s orders to reduce the American presence in Afghanistan and Iraq, and reflected the president’s longstanding desire to end long-running military engagements against Islamist insurgencies in failed and fragile countries in Africa and the Middle East, a grinding mission that has spread since the Sept. 11, 2001, attacks.