The sources said the suicide bomber struck near a busy minibus station as he targeted troops preparing to cross a checkpoint fortified with concrete blocks and manned by security officers.
"The blast was caused by a suicide bomber who was trying to strike a military convoy that was passing by the area," Somali police spokesman Sadık Dudishe told AFP.
"We have confirmed that two people were killed and 23 others wounded, some of them among the military personnel," Dudishe said.
Mohamud Adan, a police officer near the blast site, said the bombing occurred in a dense civilian area where people board minibuses for Afgoye, 25 kilometres (15 miles) northwest of Mogadishu.
A witness said the bomber was targeting a military vehicle but instead inflicted civilian casualties when his device detonated too early.
"I saw five dead people, most of them were riding on a minibus," witness Fadumao Mohamed said.
Another witness, Dahir Shine, said several minibus passengers were killed or wounded.
"I saw seven wounded people", Shine added.
Nobody immediately claimed responsibility for the attack.
The Al-Shabaab group, which is linked to Al-Qaeda, has been fighting to overthrow Somalia's federal government since 2007 and launches regular attacks against government and civilian targets.