Denis Allex, a French soldier held hostage in Somalia since 2009, has been killed during a raid by French troops trying to rescue him.
Two other French soldiers and 17 Somalian fighters were killed, the French ministry said in a statement: "Faced with the intransigence of the terrorists, who refused to negotiate for three and half years and who were holding Denis Allex in inhumane conditions, an operation was planned and carried out.
"During the assault, violent combat took place. Denis Allex was killed by his captors."
Prime Minister David Cameron said in a statement on Saturday: "I would also like to send personal condolences to the families of the French hostage killed in Somalia and those of the two soldiers either killed or missing in the rescue attempt.
"Last night's tragic events underline how essential it is that we work together to combat terrorism in Africa."
Eyewitnesses in the town in the lower Shabelle region heard fighting during the night, then saw bodies on Saturday morning.
Up to four helicopters were involved in the operation, they said.
'Series of explosions'
Mohamed Ali, a resident of Bulo Marer, told AP news agency by phone: "We heard a series of explosions followed by gunfire just seconds after a helicopter flew over the town.
"We don't know exactly what happened but the place was an al-Shabab base and checkpoint."
Another resident of the town, Idris Youssouf, told AFP that details were sketchy because the attack had happened at night.
"But this morning we saw several corpses including that of a white man," he added.
'Three civilians dead'
He said three civilians had also died in the fighting.
According to the French defence ministry, 17 militants were killed in the fighting.
The French government has previously said the two men were in the Somali capital to train local forces before being kidnapped.
After his abduction al-Shabab issued a series of demands, which included an end to French support for the Somali government and the withdrawal of African Union peacekeepers, whose 17,600-strong troops are helping battle the rebels.