The decline of Mogadishu and the cities in the Greater Somali region in general from 1600-1700s, has to do with the Oromo invasion that cut into and disturbed the interior trade routes .
@AbdiBashirJR mentioned many camels being slaughtered everyday during Ibn Batutas visit, there was thousands of camels in caravan passing through the interior to the coast , connected to trade routes carrying goods.
Drawing of a camel Caravan going to Mogadishu in the 1800s
So the interior trade routes was a vital lifeline to the city.Out of the 5 gates that surrounds the city, 4 of them go to different routes into the interior , and most of the town inhabitants act like middle men that have clan connections to the interior producers .
The second reason is Portuguese trade blockage in the Indian ocean. The Omanis came under the Portuguese for a century or so, while our ancestors kept the latter at bay and nearly extinguished their continental rival, Abyssinia. The price our ancestors paid in the process was that their trade network and cities were significantly reduced in stature and wealth. While Muscat benefited from their ability to trade under the Portuguese domination. Only when the latter were at their weakest, did Omanis find the opportune moment to shake off their rule
And its only then that they assumed a role on the East African coast that prior to the Portuguese presence had been contested between Mogadishu and Kilwa
Whereas for Somalia most of the 1800s was actually a revival period , rebuilding/repairing of trade routes and diplomatic channels , that resulted in urban growth, rural agricultural production and trade increase like you said by early 1900s Mogadishu had a population of 30.000 -40.000.
The town was a major outlet for agricultural exports:
The omanis or Zanzibari's didn't have real authority over Mogadishu. Numerous travelers, including Guillain in the 1840s, Rigby in the 1860s, and Kirk in the 1870s, observed that the Zanzibari sultans exerted no authority over coastal southern Somali cites.
The Omanis/Zanzibaris, established a trading fort under the permission of Geledi, local Somali authorities to protect their own traders and economic interest, they ended up leasing or selling it later on.
Imam Mohamed opposed the proposal, while Sultan Ahmed of Geledi advocated for it, believing that a Zanzibari trading station would enhance economic opportunities by facilitating the export of agricultural produce.
The governors they sent out were largely a ceremonial ones,
The city itself was governed under council of elders internally. That represented the different lineages that made up the inhabitants.
What Zanzibaris were used for was more or less a political leverage tool between internal power struggles between the inhabitants (succession rulership etc) and also between the Imam Yaqubi and the sultan of Geledi.
The late 1800s and early 1900s actually represented a period of impoverishment and economic decline for Mogadishu and coastal towns because of the Italians and British East India company. (This also happened up north as well). Part of colonization was to appropriate Somali commercial activity or dismantle it to weaken us. What they did to us was similar to what happened to Algerians vs French.
''The period after 1880s was fraught with social and economic crisis''