The chief justice was reportedly shot in the legs and shoulders on Wednesday.
According to police, the gunmen managed to flee the scene.
Somalia has been without an effective central government and has descended into chaos since 1991, when warlords overthrew former dictator Mohamed Siad Barre.
The weak Western-backed transitional government in Mogadishu has been battling al-Shabab militants for the past five years.
Somalia is one of the countries generating the highest number of refugees and internally displaced people in the world.
HSH/JR