The U.S. provided advice, assistance and aerial surveillance during the mission, the statement said.
Another U.S. service member in Somalia was killed in May 2017 during an operation about 40 miles west of Mogadishu.
The troops had been on a mission to clear al-Shabaab from contested areas as well as villages the militants controlled, "and establish a permanent combat outpost" to expand the reach of the Somali state, the USA military's Africa Command said in a statement.
A Defense Department official told CBS News the attack was by the al Shabaab terror group.
Names are being withheld pending next-of-kin notification, officials said in a statement, adding that the command's thoughts and prayers are with the families and friends of the service members.
He said the attack was in the southern town of Kismayo.
The attack, about 50 kilometers north of the port city of Kismayo, came as US forces were helping Somali and Kenyan troops construct the outpost, part of a multiday mission to help clear the area of militants with the al-Shabab terror group, according to USA military officials.
The US casualties are the first confirmed in Somalia since May 2017 when one soldier was killed and two others wounded in a gun battle with al-Shabab west of Mogadishu.
The death of the USA soldier in Somalia was the second in the last two years.
This is the second US military fatality in Somalia in the previous year. The injured have been treated and discharged to a U.S.embassy medical team in Kenya, the U.S. Africa command said Saturday.
In early 2017, President Trump approved expanded military operations against al-Shabab, leading to an increase in USA military personnel to more than 500 and the launch of dozens of drone strikes.
Africom said a large force comprising of about 800 forces from Somali and allied forces came under mortar and small-arms fire while conducting a multi-day operation on Friday afternoon, about 350 kms southwest of Mogadishu.
Whether U.S. forces in other parts of Africa have been asked to take similar precautions is not clear.