A security official said: "An Ethiopian sharpshooter on a rooftop fired a machine gun at the car, instantly killing the suicide bomber and blowing up the car, which was filled with explosives."
He said the would-be attacker had already run two blockades when Ethiopian and government soldiers fired at his car.
Salad Ali Jelle, the Somali deputy defence minister, said: "This was a terrorist act."
Meanwhile, a Muslim group claimed has responsibility for the Sunday night bombing of the prime minister's home, vowing to continue attacks until "occupiers" left Somalia.
A group calling itself the Mujahidin Youth Movement said in a statement on the internet that "a lion ... our brave brother Abdul-Aziz Mohammad Semter ... carried out a heroic martyrdom operation at the residence of the apostate prime minister.
"Your brethren at the Mujahidin Youth Movement are pressing on with their holy fight against all occupiers and apostates."
The statement could not be immediately verified but was on a site used by al-Qaeda and other armed Muslim groups.
Gedi - who has now survived four attempts on his life in the past two years - blamed al-Qaeda for the attack.
At a news conference on Monday, Gedi said: "Suicide bombers linked with terrorism and the al-Qaeda network attacked my residence to eliminate me, my family and all the government officials who were in a meeting with me."
He said the attack was meant to disrupt a peace and reconciliation conference planned by the transitional Somali government for later this month.
The conference has already been delayed several times because of violence.
On Friday, Somali forces and shelling from a US warship reportedly killed eight foreign fighters in a remote, mountainous northeastern area of the country.
Gedi's government, which is due to pave the way for elections in 2009, was established in 2005 in the 14th attempt to bring central rule to Somalia since the 1991 fall of Mohamed Siad Barre's government ushered in anarchy.
But despite two major offensives against fighters of the Islamic Courts Union in Mogadishu earlier this year, which killed at least 1,300 people and sent scores of thousands fleeing the city, the anti-government groups have not been defeated.