Twin car bombings and subsequent gunfire has left at least 38 people dead and 20 others wounded in the Somali capital of Mogadishu, medical workers said.

A loud explosion reportedly took place on Friday, followed by a second blast and sustained gunfire which broke out near the president's residence. The second blast took place beside a popular hotel.

The Aamin Ambulance service, which confirmed the death toll, transported victims to the hospital.

The armed group al-Shabab claimed responsibility for the attack.

"As far as we know, there has been one blast beside the presidential palace, and another close to a popular hotel," Major Mohamed Ahmed, a police officer, told local media.

AAMIN Ambulance transported Victims to Hospital caused car explosion near Fiyad Hotel the more information will notes as soon as possible pic.twitter.com/L4rTgi20Ek — Aamin Ambulance (@AaminAmbulance) February 23, 2018

The attack comes a day after the Somali government had issued a "terror warning" in the capital.

The explosions shattered a months-long period of calm in Mogadishu, which is often the target of attacks by al-Shabab.

In December, 18 police officers were killed when an al-Shabab fighter blew himself up inside the country's main police academy in Mogadishu.

The capital was also the target of a truck bombing in October that killed 512 people in the deadliest attack in Somalia's history. The Somali government blamed that blast on the al-Qaeda linked al-Shabab.