US President Donald Trump has ordered the withdrawal of nearly all US troops from Somalia by 15 January, the Pentagon has said.
The US has about 700 troops in the country helping local forces battle al-Shabab and Islamic State militants.
US officials said some of the troops would move to neighbouring countries, allowing for cross-border operations.
In recent months President Trump has issued similar orders to reduce US forces in Iraq and Afghanistan.
He has long called for US troops to come home and has criticised US military interventions for being costly and ineffective.
The withdrawal order - which would see troops redeployed just days before Mr Trump leaves office - reverses the policy of former US defence secretary Mark Esper, who was sacked last month and favoured maintaining the US presence in Somalia.
A Pentagon statement said that the order to "reposition the majority of personnel and assets out of Somalia by early 2021" did not signify a change in US policy.
"We will continue to degrade violent extremist organisations that could threaten our homeland while ensuring we maintain our strategic advantage in great power competition," it said.
Bombings and assassinations
Some experts have warned that a US withdrawal could embolden militants in the Horn of Africa region.
Somalia has suffered decades of political instability but in recent years a peacekeeping force from the African Union along with US troops have reclaimed control of Mogadishu and other areas from al-Shabab - an al-Qaeda affiliate.
The group has fought for more than 10 years to impose a regime based on a strict version of Sharia law, and often attacks civilian and military targets, carrying out bombings and assassinations in the capital.
Defectors from the group have described how difficult it was to escape its clutches and become deradicalised. A report in October said the group raises as much revenue as the country's authorities, using intimidation and violence to demand money from businesses and farmers.
Major impact on battle with militants
By Mary Harper, BBC World Service Africa editor
Prominent Somalis have criticised the Pentagon's announcement that nearly all the 700 American troops will leave Somalia by mid-January, on President Trump's orders.
Senator Ayub Ismail Yusuf described the decision as "extremely regrettable". US troops, he said, have made a "huge contribution" to the training and operational effectiveness of Somali troops.
This is certainly the case with the Danaab or Lightning Brigade, an elite force of Somali commandos trained by the US, who have proved especially effective against al-Shabab.
Presumably America will continue to conduct airstrikes against the militants from bases in neighbouring countries.
But the near complete drawdown of on-the-ground forces will have a major impact on the battle against Islamist militants, who continue to control vast swathes of Somali territory.
Last month, US government inspectors advised against a withdrawal from Somalia, saying local forces would not be able to resist threats from militants without US support.
Those US forces left in Somalia will be based in the capital Mogadishu, the Pentagon said.
US presidents have been wary of intervention in Somalia since 18 special forces soldiers died fighting militias in Mogadishu in 1993, a battle dramatised in the film Black Hawk Down.
However, despite his 2016 election campaign to disentangle the US from "endless wars", President Trump has expanded military action against al-Shabab, mainly in the form of air strikes.
Last month, US defence officials said American forces would be further reduced in Afghanistan and Iraq.
Those in Afghanistan will be cut from about 5,000 down to 2,500 by mid-January, officials said. In Iraq they will be reduced from 3,000 to 2,500.