The fighters said they were trying to disrupt protracted parliamentary elections - part of efforts to rebuild the fractured nation after decades of war. The three-month vote is due to end on Dec. 29.
Gunfire rang out after the blast at Mogadishu Port, Mohamed Hussein, a worker there, told Reuters. Two others said work had been halted and staff sent home.
The bodies of victims lay strewn outside the capital's terminal in a street filled with rubble from damaged tea shops.
"At least 29 civilians died and 50 others have been injured in the blast. We believe it was a suicide truck bomb," police officer Colonel Abdikadir Farah told Reuters.
Al Shabaab's military operation spokesman, Sheikh Abdiasis Abu Musab, told Reuters the blast was aimed at police officers stationed close to the port.
"We killed 30 security forces and injured 50. We targeted them because they had been trained to provide security at so-called elections,' he said.