A witness said the prime minister's officials jumped off their vehicles and fired into the air after hitting the landmine.
"The security men arrested several people in the area," said Mohamed Abdi Abdulahi.
No one was injured in the incident in the Juba neighbourhood of northern Mogadishu, the third attempt on Gedi's life since he was nominated to head Somalia's transitional government in 2004.
Gedi was earlier at the Mogadishu International Airport, from where the bodies of the four Ugandan peacekeepers killed by a roadside bomb on Wednesday left for Kampala.
US appoints envoy
Also on Thursday, the US government announced that it appointed a special envoy to Somalia.
John Yates, the new envoy, is a career diplomat for 40 years who already leads the Somalia unit at the US Embassy in Nairobi in neighboring Kenya.
"The United States is committed to helping Somalis develop their national institutions and overcome the legacy of violence and disorder of the past," Condoleezze Rice, the US secretary of state, said in a statement announcing Yates' appointment on Thursday.
Yates, a former ambassador to Cameroon, Equatorial Guinea, Benin and Cape Verde, will continue to work from Kenya as Somalia is not safe enough for a US diplomatic presence, a state department official said.