A statement from the US Africa Command said the military member was killed on Thursday during an operation near Barii, about 64km west of the capital, Mogadishu. The statement said US forces were conducting an "advise-and-assist" mission with Somalia's military.
A spokesman for al-Shabab, which is fighting to overthrow Somalia's western-backed government, said that its fighters had killed "several" US soldiers.
"[The US soldiers] came in helicopters. Our Mujahideen fighters responded to the attack," al-Shabab's military operations spokesman Abdiaziz Abu Mus'ab told Al Jazeera.
"We killed several American fighters. We also wounded many. We captured weapons from them too. They ran back to their helicopters."
In the past, al-Shabab has exaggerated the number of soldiers it has killed during clashes.
A Somali intelligence official confirmed the US military operation, saying US forces in helicopters raided an al-Shabab hideout near the Somali capital on Thursday night and engaged with fighters.
The official, who spoke to the Associated Press news agency on condition of anonymity because he was not authorised to speak to the media, said the helicopters dropped soldiers near Dare Salaam village in an attempt to capture or kill fighters in the area.
The official said the fighters mounted a stiff resistance against the soldiers.
According to the US army, the fatality appears to be the first death of a US serviceman in combat in Somalia in more than two decades.
US Africa Command spokesman Patrick Barnes said that "we do not believe there has been a case where a US service member has been killed in combat action in Somalia since the incident there in 1993," according to AP.
The US pulled out of Somalia after 1993, following the "Black Hawk Down" incident, when two helicopters were shot down in Mogadishu and bodies of Americans were dragged through the streets.
Both the US and Somalia in recent weeks have declared new efforts against al-Shabab.
Somalia's new president Mohamed Abdullahi Mohamed last month declared a new offensive against al-Shabab, which is based in Somalia but has claimed responsibility for major attacks elsewhere in East Africa - including in neighbouring Kenya.
US President Donald Trump has approved expanded military operations against al-Shabab, including more aggressive air raids and considering parts of southern Somalia areas of active hostilities.
Also last month, the US military announced it was sending dozens of regular troops to Somalia in the largest such deployment to the country in roughly two decades. The US Africa Command said the deployment was for logistics training of Somalia's army.
The US in recent years has sent a small number of special operations forces and counter-terror advisers to Somalia and has carried out a number of air strikes, including drone strikes, against al-Shabab.
Source: Al Jazeera and news agencies