President Sheikh Sharif Sheikh Ahmed of Somalia escaped unharmed from an attack by al-Shabab after a visit to a newly captured town on Tuesday.
Al-Shabab, which joined al-Qaeda this year, has claimed responsibility.
At least one soldier was killed and four wounded when al-Shabab gunmen opened fire on the presidential convoy on the road between Afgoye and Mogadishu. They were repulsed by government and African Union troops.
Afgoye is 30km north-west of Mogadishu. AU troops said they had made the route secure on Monday.
Officials say the interim president returned safely to the capital.