Abdifatah Barre, the deputy district commissioner of Mogadishu's Wadajir district, was shot dead in his car.
"Gunmen opened fire on the car of the deputy district commissioner this morning. The official died and the gunmen escaped," Major Ibrahim Hussein, a police officer, told Reuters.
Al-Qaeda-linked al-Shabaab claimed responsibility and threatened more attacks.
"We killed the deputy district commissioner and we shall continue killing the enemies. This is part of our operation in Mogadishu," Sheikh Abdiasis Abu Musab, its military operations spokesman, told Reuters.
Al Shabaab carries out attacks frequently in Somalia and in neighbouring countries. It wants to impose its strict interpretation of Islamic law in the region and overthrow the Somali government, which is backed by Western donors and African peacekeepers.
Late on Monday, gunmen from the group stormed a police station in the country's semi-autonomous region of Puntland and killed three policemen.