According to Reuters, the man, Mohamed Qanuuni, had only recently assumed the post of deputy commander in charge of anti-terrorism activities, according to an intelligence officer who asked not to be named. He took the job after his predecessor who was killed in a similar manner two months ago, police said.
"The gunmen blocked a car in front of his car and then opened fire on him," said Major Bile Hussein, a police officer. "The man who was killed today had replaced another senior officer who had been killed two months ago."
Somali officials have been on high alert since the leader of al Shabaab, Ahmed Godane, was killed in a U.S. strike earlier this month.
Al Shabaab, which wants to impose its strict version of Islam on Somalia, quickly appointed a new leader and pledged revenge attacks against the United States, as well as on Kenya and Uganda, East African countries it has attacked before.
On Saturday, Al Shabaab took responsibility for the shooting, and claimed a second person had also been killed in the attack.
"We have killed the deputy anti-terrorism force commander and another colleague of his in Mogadishu today," Sheikh Abdiasis Abu Musab, spokesman for al Shabaab's military operations, told Reuters.
"He was one of those who eavesdrop on telephones," he said.