The US military says its forces operate on an "advise and assist" mission with the Somali National Army.
The incident is the first known death of a USA servicemen in Africa since the ambush in Niger last October.
In a statement, the US military said on Friday one local soldier was also wounded in the attack that also involved Somali and Kenyan forces.
Al-Shabab quickly claimed responsibility for the attack on Telegram via its Shahada News Agency.
Names of the soldiers have not been released while the US notifies next of kin.
Friday's joint operation, part of a multi-day mission including about 800 Somali and Kenyan troops, aimed to clear al-Shabab from contested areas.
Another U.S. servicemember in Somalia was killed in May 2017 during an operation about 40 miles west of Mogadishu. Earlier Friday, the U.S. Africa Command issued a statement in response to allegations that civilians had been killed in a May 9 operation, saying a "thorough review" found the allegations to be "not credible".
But U.S. military officials said this week that no decision had been made.
A suicide auto bomb explosion at a military base in Somalia injured seven soldiers late Saturday, a military official said, and Islamist group al Shabaab claimed responsibility for the attack.
About 500 USA troops are deployed in Somalia. According to one military official familiar with the attack, the Special Forces soldiers had less than a month left on their deployment.
"Our strategy in East Africa is to build partner capacity to ensure that violent extremist organizations, who wish harm in the region, wish harm on the European continent, and ultimately wish to harm the United States, are contained", General Thomas Waldhauser, Commander, U.S. Africa Command, said in a statement Saturday.
That prudence included steps to make sure that USA forces in Niger had access to more armored vehicles, additional firepower, and more drones for surveillance and reconnaissance.
"The population in the region had historically supported the government, and other Somali forces had prepared for this mission by coordinating heavily with and securing the support of local authorities ahead of time", the statement said.