Two Somali MPs with dual British citizenship were killed by al-Shabaab terrorists in an attack on a hotel in Mogadishu.
Foreign Secretary Philip Hammond, who was visiting Somalia, sad the extremist organisation "cannot be allowed to take hold" in the east African country.
Somali security forces ended the overnight siege by extremist gunmen at the Ambassador Hotel which killed at least 15 people, including the two MPs.
The assault started when a vehicle laden with explosives detonated outside the hotel on Wednesday evening and then three militants stormed inside the building.
Mr Hammond expressed his sympathies for the families of the MPs - understood to be Abdullahi Jama Kabaweyne and Mohamoud Mohamed Gure - and others who lost their lives as he visited Mogadishu.
The UK is deploying about 70 military personnel to the country to help support the African Union's mission in Somalia.
Following talks with president Hassan Sheikh Mohamud and prime minister Omar Abdirashid Ali Sharmarke, he said: "A secure and stable Somalia is the UK's top priority in east Africa and is in our own national security interests.
"Britain is providing security support to Somalia and to the African Union to tackle al-Shabaab, a terrorist organisation that cannot be allowed to take hold.
"This week's al-Shabaab attack is yet another reminder of the importance of the work we are doing to support Somalia and to increase security across the region."
The UK is the only European Union country to maintain an embassy in Somalia and has taken a lead in co-ordinating international support for the fledgling Somali government, including around a £200 million commitment to security reforms.