Al-Shabaab suicide bombing kills more than 20 in Somalia
"The tally we have made so far indicates that more than 20 people, most of them civilians, were killed in the blast," said Ibrahim Mohamed, a Somali police commander.
It is unclear how many people had been in the vicinity of the blast, but the commander said the toll is expected to rise as several hospitals across the city opened doors to dozens of the dead and wounded.
Abdikadir Abdirahman Adem, director of Mogadishu's AMIN ambulance service, said his staff had transported 48 injured people to hospital.
"The bomber targeted a civilian area. There were porters and other small-scale traders in the area when the blast occurred," said Mohamed.
Mogadishu residents described hearing a huge blast outside the city's busy seaport before seeing a plume of smoke that rose above the skyline.
A spokesman for the city administration, Abdifatah Omar Halane, said investigations were under way, as an al-Shabaab militant statement distributed on Telegram showed the group had claimed the attack.
The militants' statement said the target was a military base close to the port and claimed to have killed "nearly 30", although the group frequently exaggerates the number killed in its attacks.
The militant group is fighting to overthrow the internationally-backed government of Somalia and regularly stages deadly attacks on government, military and civilian targets in the capital and elsewhere in the war-torn country.
Sunday's attack took place close to the entrance to city port and was large even by Mogadishu's standards.
"I have never witnessed such a blast which caused so much devastation," said local resident Abdukarim Osman.
The attack comes as Somalia is in the process of electing a new government with the much-delayed presidential vote due on December 28.
Al-Shabaab have increased their violence against the state in recent months, in an attempt to disrupt local elections.