Before being blind-folded and shot, both men expressed their anger at the verdict and shouted « God is great ».
Seven alleged comrades were sentenced to 15 years in prison.
A BBC correspondent in the capital says it is not clear whether the men faced a trial before their executions. A government spokesman would not comment.
« After the killing of our officers, we were given some of the names of the murderers, we tracked them down immediately and captured these two guys, who were later convicted of being the killers, » said an officer, who is a member of the National Security Services (NSS), on condition of anonymity.
Meanwhile, a roadside bomb has gone off near a convoy carrying Mogadishu mayor, a former warlord, Mohamed Dhere, although no-one was hurt.
Such attacks occur on an almost daily basis and are usually blamed on Islamist insurgents and fighters from the Hawiye clan – the largest in Mogadishu.
« It was an assassination attempt, but luckily he survived, » his spokesman said.
Hawiye leaders say they will not attend a reconciliation conference due to be held next month.
Earlier, government soldiers staged protests in two towns to demand payment after going seven months without any wages.
There was an exchange of gunfire in a barracks in Jowhar, near the capital, but there were no casualties.
Angry soldiers have taken control of a police station in the central town of Beledweyne.
The government is trying to assert its control after ousting Islamists from the capital, with Ethiopian backing.
Somalia has not had an affective national government for 16 years, since when rival militias have been battling for control of different areas.