Prime Minister Mohammad Ali Gedi was unhurt, but the blast on Sunday sharply raised tensions in a city controlled largely by his political foes.
Government aides said a landmine caused the explosion.
Unidentified assailants threw a hand grenade under Gedi's car as it passed seconds before the landmine blast in an apparently coordinated attack, a government source said.
The blast set at least one vehicle ablaze in the convoy that was ferrying Gedi and one of his deputies from an airstrip to the lawless Indian Ocean city.
"Five seconds after the blast, there was an exchange of fire, I don't know who between," a wounded man who had been riding in the convoy told Reuters from his hospital bed.
"The tyres on our car were shredded by bullets."
"It struck the car directly behind the prime minister's car, and that vehicle burst into flames," Ali Nur Sahal, an aide to Deputy Prime Minister Hussein Aideed, told Reuters.
"We believe it was a landmine."
Aideed, who is also Interior Minister, had welcomed Gedi at the airstrip and was also travelling in the convoy. He too was unhurt.
"This was an apparent attempt at disrupting the effort at consolidating the outcome of the reconciliation conference"
Mohammed Ali Faum,
African Union envoy to Somalia
Gedi, based in the town of Jowhar 90km north of Mogadishu, was visiting the Indian Ocean city for only the second time since he was appointed as part of a Transitional Federal Government (TFG) formed at peace talks in Kenya in 2004.
"This was an apparent attempt at disrupting the effort at consolidating the outcome of the reconciliation conference," Mohammed Ali Faum, the African Union envoy to Somalia, said of the peace talks where Gedi's government was formed.
The TFG is the 14th attempt to reinstate central government since the 1991 toppling of former dictator Mohamed Siad Barre. But the administration has found it difficult to impose any authority on the tribal leaders who still control much of the country.
Most of the government's ministers work from Jowhar, arguing that the capital is too dangerous. But a dissident faction is based in Mogadishu, as is Aideed, who has tried to steer a neutral course between the two groupings.
Experts say both factions of the government are gearing up for a military showdown, and a UN report by a panel of experts said government ministers on both sides had bought large amounts of weapons in recent months in breach of a UN arms embargo.
On Gedi's first visit to Mogadishu in May, 14 people were killed and about 60 wounded in an explosion at a stadium where he was addressing a crowd. Again, he was unhurt. The cause of that blast has not been established.