Maḏāhib (maḏhab singular) are Sharīʿa doctrinal schools. In the 7th century, Muslim juristic knowledge started as study circles (halaqas) in which a pious Muslim scholar – surrounded by people – would debate religious issues and teach interested students. Without an ecclesiastical hierarchy, there was no institutional monopoly over divine truth or divine intent by any scholar. This environment fostered and actually encouraged different interpretations of the law. The knowledge and production of legal doctrine began in these circles that gave birth to the Maḏāhib (Sharīʿa doctrinal schools).
This legal pluralism provided flexibility and the ability by design to accommodate different societies and regions. It is believed that dozens or even hundreds of different scholars established their own doctrinal schools or schools of law with students and followers. By the 11th or 12th century, these schools went through a process of consolidation and/or extinction due to objective political, social, economic, and intellectual factors which led to only four surviving schools in Sunni law: Ḥanafī, Shāfi'ī, Mālikī, and Ḥanbalī.
In most intellectual centers around Muslim lands, these doctrinal schools thrived. It was not unusual that you would have a Ḥanafī scholar teaching while another Shāfi'ī scholar is teaching another halaqa at the same mosque. The Ottoman Empire adopted the Ḥanafī school as the Empire's doctrinal school. For this reason, it became well-entrenched in Muslim lands - but that did not eliminate the other schools from thriving and taking root all over the Muslim world.
The Arab Persian Gulf countries follow the Ḥanbalī School of Law; North Africa, such as Morocco, Tunisia, and Algeria, follow the Mālikī School of Law; Sunni Muslims in Turkey, Iraq, Egypt, Syria, India, Pakistan, and Afghanistan as well as Central Asia tend to follow the Ḥanafī School of Law. Palestine, East Africa, Indonesia, and Southeast Asia predominantly follow the Shāfi'ī School of Law.