Adbikarin Hussein Guled said seven of the casualties were al-Shabaab insurgents and two were government officials -- identified as the secretary to the prime minister and the former deputy national intelligence agency chief -- CNN reported.
A U.N. envoy said in a post on Twitter that Somali President Hassan Sheik Mohamed told him by phone he was not hurt.
"President just called me to say he's unharmed. Attack on Villa #Somalia had failed. Sadly some lives lost. I condemn strongly this terrorism," tweeted Nick Kay, the U.N. special representative of the secretary-general for Somalia.
An explosive device was detonated at the gate, and was followed by an exchange of gunfire between security guards and the militants, CNN said.
The number of injured was not reported.
Al-Shabaab, a group linked to al-Qaida and designated a terrorist organization by the United States, claimed responsibility for the attack. The organization's military operations spokesman said the group had achieved its objectives in the attack.