Officials say the attack is now over and the building has been secured.
Al-Shabab opposes the UN-backed administration and wants African Union forces to leave Somalia.
Last month, the al-Qaeda-linked insurgents launched a two-day siege at a hotel in Mogadishu.
And earlier this month, al-Shabab sent shockwaves through neighbouring Kenya when it killed 148 people in an attack on Garissa University.
Tuesday's attack began when a suicide bomber struck the walled compound - which houses the education ministry and other government buildings - in a car laden with explosives.Gunmen then streamed into the hole in the building's defences and started shooting.
Somali government spokesman Ridwan Abdiweli said a total of 17 people died in the attack.
That includes eight civilians and two soldiers - one from the African Union Mission and the other from the Somali military. Seven al-Shabab fighters also died.
Omar Mohamed, a trader who works near the scene, told Reuters news agency that he was thrown off his chair when the blast went off.
He said attackers sprayed gunfire at security forces as they battled to retake the building.
Witness Idil Malim told the AFP news agency he had seen several dead bodies lying at the scene.
Shooting and the wail of ambulances could be heard, and many injured people were carried away to hospital. Public buses sat abandoned in the street close to the ministry compound.
The ministry's director-general, Ismail Yusuf, was among the injured, state radio reported.
It said the head of security at the ministry, who it named as Sandheere, was among the dead.
Al-Shabab militants often launch attacks in the city and have previously used car bombs to initiate attacks on fortified buildings.