Somalia's al Shabaab Islamist group launched a suicide bomb attack on a hotel in the center of Mogadishu on Saturday before fighters stormed inside, police and the militant group said.
Police said at least 15 people had died, including guards at the site, civilians and militants. Others were wounded.
Gunfire had echoed round the seaside capital after the blast and ambulances raced to the scene. Police later said fighting had ended but they were searching the site for militants.
"We attacked the hotel which was frequented by the apostate government members," al Shabaab military operations spokesman Sheikh Abdiasis Abu Musab told Reuters. He said at least 20 guards and civilians were killed.
Al Shabaab, which frequently carries out attacks in the capital in its bid to topple the Western-backed government, often gives casualty numbers that are higher than figures announced by officials.
Police said the initial blast was caused by a suicide bomber before fighters stormed into the Nasahablood hotel, leading to a heavy exchange of gunfire. Officers said some people had managed to escape through the rear of the building.
"The operation has now ended but we are still combing the building for any possible militants who are hiding," Major Ali Mohamed, a police officer, told Reuters.
The United States and other Western countries have been among the biggest donors to the government in Mogadishu as it slowly rebuilds after years of conflict and prepares for parliamentary and presidential elections later this year.
"We strongly condemn this attack," a US State Department official said in a statement. "We remain committed to Somalia's security and stability, and are proud to stand side-by-side with Somalia in the fight against terrorism."
The vote planned for August will be by limited franchise rather than one-person-one-vote, which officials and diplomats say would be too difficult while still fighting an insurgency.
Muslims in Somalia and around the world are observing Ramadan. In previous years, al Shabaab has often intensified attacks during the fasting month, frequently picking targets where people gather just before or after breaking the fast.