The gunmen had approached the restaurant from Lido beach on Thursday evening, firing on diners.
Twenty people were killed. Militants also detonated two car bombs nearby.
Al-Shabab said it was behind the attack. The group has carried out frequent assaults on the Somali capital.
Who are al-Shabab?
Al-Shabab divided by IS call
Lido beach: Sun, surf and... grenades?
Troops then besieged the attackers at the restaurant for eight hours. It was not immediately clear how many of the militants were killed or captured.
The Somali authorities said the leader of the attack had been arrested.
Prime Minister Omar Abdirashid Ali Sharmarke condemned the killings as "barbaric".
There are varying reports about how the attack started but it appears that as the first car bomb went off, at around 19:30 local time (16:30 GMT) on Thursday, the gunmen stormed the area.
"They randomly fired at the people sitting near the beach before entering the restaurant,'' witness Ahmed Nur told Associated Press.
Twenty people were injured in addition to those killed, police said.
After the evening prayer, I went to Lido beach, in particular Beach View restaurant. When I was there about 30 minutes, and I was spending time with my friends including journalists, we saw bullets raining on us.
A good number of people were seated around me, there was a wedding ceremony at the hall of the restaurant. We started running towards the door, but before I reached it we heard a loud explosion.
I saw some people jumping from the second floor. Bullets were flying all over the place, and there was another explosion. Then everyone got confused. I personally saw three wounded people. I did not see those who have been harmed by the explosions. I think the car was parked near the main door of the restaurant.
Lido beach, on the northern edge of Mogadishu, attracts thousands of mostly young Somalis who come to relax and enjoy the beach and the surf.
Several restaurants have opened up along the beach front in recent years, and people would have been sitting out in front of them when the attacks began.
Al-Shabab, which has links to al-Qaeda, was ousted from Mogadishu in August 2011, but still has a presence in large areas of southern Somalia.
The group stormed an African Union military base last week in southern Somalia, killing dozens of Kenyan troops.

The Gunners have a 2-1 lead over Potsdam from the first leg and are favourites to reach the quarter-finals.
Captain Katie Chapman said: "We dominated Thursday's game and could have put the tie to bed if we'd finished a few more chances.
"We'll go to Germany believing we can get the result we need to go through."
Arsenal, who won the competition in 2007, are in their 11th season in European football's elite competition and have never failed to reach the last eight.
But twice-winners Potsdam, who last lifted the trophy in 2010, have likewise always made it through to at least the quarter-finals.
Chapman, one of the first-leg goalscorers, added: "Potsdam are one of the very best teams in Europe and it's going to be really tough over there.
"But we've beaten them once, and if we can maybe get an early goal it will make it very difficult for them."
Somali security forces have retaken control of a popular beachside restaurant in Mogadishu after a deadly attack there by Islamist militants.
[NEXT_CONCEPT]
Arsenal Ladies will aim to become the first British side to knock out German opposition in the Women's Champions League on Wednesday.