When the Messenger of Allah (Peace be upon him) died, there was no clear instance on what is to do and what not. The sahaba (May Allah be pleased with them) were wide spread in the Muslim countries where they have been teaching Islam and Sunna and Quran, while many died in Medina and Hijaz some have been living in 'Iraq and a-Shaam. At the time of sahaba there have been 4 major "scholars" known by the Abadillah the 4 Abdullah's : Abdullah ibn 'Omar, Abdullah ibn Masu'd, Abdullah ibn al-'Abbas and Adbullah ibn 'Amr ibn al-'Aas. Of course many other sahaba have been there too but those four have had the most influence. For example maybe a sahabi like abu Huraira knew and collected a lot of hadiths (even after the death of the Prophet) but an other had better skills to conclude rulings from them. Scholars such as ibn Hazm counted 18 sahaba who had what we may call an own madhhab or ijtihad (opinion), an-Nasa-i counted 22! While among the tabi'in the amount was approximately doubled.
A division in opinions began already at the time of sahaba and they have had influence on the tabi'yn and so on. But they have had also some kind of consensus in known cases. You must understand, that at this time nobody had a good database to search for a hadith so they all could only conclude rulings from the given data or information they had as this was what they have been taught (See for example in Sunan abi Dawod, Sunan a-Nasa-i, Jami' at-Tirmidhi). For some more or less obvious reasons the sahaba who have been living in the 'iraq had much more to deal with new Problems, so because they have been less in quantity they may also have less sources for hadith which made them with the time more and more flexible and able to conclude rulings from the "few" sources they had and build up juridic jurisprudence. While those in Hijaz haven't had that quantity of "new" problems. So in 'Iraq a ra'y (opinion based) jurisprudence school has developed with the time, while in al-hijaz the school was mainly based on the original sources -mainly the hadiths and sunna- (ahl al-hadith).
There's no origin of that concept there are factors:
one of them was the necessity to fix a madhhab, as due to the fact that each scholar had his own opinion, ordinary people were seeking guidance, as everybody was claiming that he was following the Qur'an and sunnah and at the time of the tabi'in there already have been about 40 mujtahid with his own opinions, which are based on these basics.
An other are political factors like the hanafi school being for a part of the Abbasid Dynasty the preferred school (Abu Yousuf the student of abu Hanifa was the highest qadhi/qazi after his master refused and has been tortured) and then after the fitnah of quran the hanbali school was much appreciated for a certain time. The maliki school was appreciated because of the knowledge of Imam Malik as his book was one of the first sahih hadith collections (It includes at least 500 hadiths which you may find in any other hadith collections beside some 200 more in the final revision and you may find in those books ahadith narrated by him which are not in the muwatta'a as it was more a book of ahkam and fatwa). The student of abu Hanifa Imam Muhammad ibn al-Hassan has narrated the muwatta' and when he used to recite from it he had always a bigger audience as in his other lessons. Note that many teachers of Imam Malik where his students later, as he was one of the first whom have been given the title Amir al-mu'minyan fil hadith, his muwatt'a is a revised summary of much more then 100000 hadiths and narrations about ahkam he has collected beside some of his fatwas. Imam a-Shafi'i was welcomed in Egypt at first as he was a student of Malik, but then more and more accepted by the former students of Imam a-Laith ibn Sa'ad. But at the time there have been many other good scholars who had reached a high level of knowledge some were even set much higher than those four like Sufyan ibn 'Oyyana, Sufyan a-Thawri, Abdullah ibn al Mubarak, al-Awzaa'i , a-Laith ibn Sa'ad.... and among the tabi'yn an even larger number of great scholars whom (maybe) had a madhhab of their own and followers.
People of the Maghreb and Andalusia after converting to Islam -via Preachers and fighters from a-Shaam- adapted at first the madhhab of Imam al-Awzaa'i (from a-Shaam) and afterwards had a strong relationship to Medina as the "city of the Messenger of Allah (Peace be upon him)" so they were among the first to become shi'a or "maliki" and they also adapted the (quran) reading of imam Nafi' as a major "standard". Of course if in a major city a qadhi/qazi following one madhhab has been appointed he would be in favor for colleagues of the same madhhab! For example: it's said that Yahya ibn Yahya al-Laithi the Andalusian scholar, who was the last one to have his muwatta' version revised by Imam Malik had good relationships to the rulers in Al-Andalus therefore it was up to him to choose the qadhi's. The same applied during the 'Abasid reign for the hanafi school due to Abu Yusuf the student of abu Hanifa who was qadi.
Now if you want to know at what time the sunni school limited the madhhabs to only four. then it's hard to say but maybe this happened somewhere between the 5th and late 6th century due to a consenting silent (as nobody tried to do ijtihad al mutlaq and all scholars only wrote comments on former madhhabi sources). At least Qadhi 'Iyad pretend in his book about the scholars of the Maliki school that in al-Maghrib one couldn't anymore find scholars of any other school then the maliki madhhab.
Note that ibn Hazm (a-dhahiri) lived only a century before in al Andalus and the later dynasty of the Almohads الموحدون encouraged ijtihad in any madhhab no matter if it was in fiqh or 'aqidah.
Some people say it was ibn Salah (others have also been named) that declared the closure of the doors of ijtihad and that only four madhhabs are exiting. But the dhahiri school still exists in the salafi thoughts.
فقيل لابن الماجشون: لمَ رويتم الحديث ثم تركتموه؟ قال: ليعلم الناس أنا على علمٍ تركناه.