Security official Mohamed Hassan said three soldiers were killed and that the bomber was believed to be from the radical Islamist group al-Shabaab.
Police spokesman Qasim Ahmed Roble said the blast impacted a settlement of people who had fled a 2011 famine, killing two children.
Unconfirmed reports said a fourth member of the security forces was also killed.
Several people were wounded, including members of the security forces and displaced people at the camp.
The suicide bomber intended to target parliament, where legislators were in session, sources said.
In an attempt to stop the bomber, police fired at him several times, Mr. Roble said, which made him blow up the car.
In a separate attack, two traffic police officers were shot dead in Mogadishu, according to local police and witnesses.
Senior al-Shabaab member Abdiaziz Abu Mus’ab claimed responsibility for both the suicide bombing and the killing of the policemen in an interview with pro-insurgent Andalus radio.
Mr. Hassan said Prime Minister Abdiweli Sheikh Ahmed was meeting with officials to beef up security measures.
At least 20 members of the security forces have been killed in Mogadishu since the beginning of Ramadan at the end of June.
Suspected al-Shabaab militants have also killed four parliamentarians over the past six months, the most recent attack being this week.
Al-Shabaab attacked the presidential palace and parliament earlier this year, leaving dozens dead.
The government’s decade-long conflict with al-Shabaab has cost thousands of lives. Weakened by an offensive by government and African Union troops, the group is increasingly resorting to terrorist tactics.
Special Reporter