Two police officers were among the dead and two armed extremists who tried to storm the traffic police premises in Mogadishu's Abdiaziz district were also shot dead, said Capt. Mohamed Hassan.
Somalia's Islamic extremist rebels, al-Shabab, claimed responsibility for the attack, which shattered a period of calm in the seaside city.
Despite being pushed out of Somalia's major cities and towns, al-Shabab, which is allied to al-Qaida, continues to carry out deadly guerrilla attacks across the country.