Why did Prophet Muhammed (SAW) go back to Madina after conquest of Makkah? Was it an order from Allah or a personal preference?
The Prophet ﷺ returned to Medina after the conquest of Mecca because he considered himself one of al-Ansār, the dwellers of Medina. He had promised them on 21 Ramadan 8 A.H., one day after Mecca surrendered, that he would live with them. The incident is documented in a long hadith in both Sahih al-Bukhari and Sahih Muslim (partially quoted below) narrated on the authority of 'Abdullah ibn Rabāh:
وَجَاءَتِ الأَنْصَارُ فَأَطَافُوا بِالصَّفَا فَجَاءَ أَبُو سُفْيَانَ فَقَالَ يَا رَسُولَ اللَّهِ أُبِيدَتْ خَضْرَاءُ قُرَيْشٍ لاَ قُرَيْشَ بَعْدَ الْيَوْمِ. قَالَ أَبُو سُفْيَانَ قَالَ رَسُولُ اللَّهِ صلى الله عليه وسلم:
مَنْ دَخَلَ دَارَ أَبِي سُفْيَانَ فَهُوَ آمِنٌ وَمَنْ أَلْقَى السِّلاَحَ فَهُوَ آمِنٌ وَمَنْ أَغْلَقَ بَابَهُ فَهُوَ آمِنٌ
فَقَالَتِ الأَنْصَارُ أَمَّا الرَّجُلُ فَقَدْ أَخَذَتْهُ رَأْفَةٌ بِعَشِيرَتِهِ وَرَغْبَةٌ فِي قَرْيَتِهِ. وَنَزَلَ الْوَحْىُ عَلَى رَسُولِ اللَّهِ صلى الله عليه وسلم قَالَ
قُلْتُمْ أَمَّا الرَّجُلُ فَقَدْ أَخَذَتْهُ رَأْفَةٌ بِعَشِيرَتِهِ وَرَغْبَةٌ فِي قَرْيَتِهِ. أَلاَ فَمَا اسْمِي إِذًا - ثَلاَثَ مَرَّاتٍ - أَنَا مُحَمَّدٌ عَبْدُ اللَّهِ وَرَسُولُهُ هَاجَرْتُ إِلَى اللَّهِ وَإِلَيْكُم فَالْمَحْيَا مَحْيَاكُمْ وَالْمَمَاتُ مَمَاتُكُمْ
قَالُوا وَاللَّهِ مَا قُلْنَا إِلاَّ ضِنًّا بِاللَّهِ وَرَسُولِهِ. قَالَ
فَإِنَّ اللَّهَ وَرَسُولَهُ يُصَدِّقَانِكُمْ وَيَعْذِرَانِكُمْ
The Ansar also came there and surrounded the mount. Then came Abu Sufyan and said: "Messenger of Allah, the Quraish have perished. No member of the Quraish tribe will survive this day." The Messenger of Allah (ﷺ) said:
Whoever enters the house of Abu Sufyan will be safe, whoever lays down arms will be safe, whoever locks his door will be safe.
(Some of) the Ansar said: "(After all) the man has been swayed by tenderness towards his family and love for his city." At this, Divine inspiration descended upon the Messenger of Allah (ﷺ). He said:
You were saying that the man has been swayed by tenderness towards his family and love for his city. Do you know what my name is? I am Muhammad, the bondman of God and His Messenger. (He repeated this thrice.) I left my native place for the sake of Allah and joined you. So I will live with you and die with you.
Now the Ansar said: By God, we said (that) only out of our greed for Allah and His Messenger. He said:
Allah and His Apostle testify to you and accept your apology.
As you can see, the Prophet ﷺ promised al-Ansār to live with them and die with them. Even then, the doubts of al-Ansār surface again just over two weeks later on the day of the battle of Hunayn on 10 Shawwāl 8 A.H. when the Prophet ﷺ gave the Meccans a higher portion of the bounty from the share that was dedicated to him (one-fifth), and al-Ansār felt that he was favoring his own tribe. They said: "May Allah forgive His Messenger. He gives [the people of] Quraish more, and he leaves us while our swords are still dripping with blood. When it is a tough time, we are called upon, then the reward goes to others."
The Prophet ﷺ heard about this incident and gathered them. He asked them about their concern, but they elected not to respond. Then the Prophet ﷺ asked them if what he had heard that they said was true, and they confirmed it. He then asked Sa'd ibn 'Ubādah about the incident, and he replied: "This group of al-Ansār have something in their hearts from how the bounty was divided. You gave your tribe and you gave a great share to the tribes of the Arabs, but al-Ansār had no share in this." The Prophet ﷺ inquired about Sa'd's stance, and Sa'd replied that he was but one of his tribe (i.e., he agreed with the opinion of his tribe).
The Prophet ﷺ then asked Sa'd (he was the leader of al-Ansār) to gather his people:
عَنْ عَبْدِ اللَّهِ بْنِ زَيْدٍ، أَنَّ رَسُولَ اللَّهِ صلى الله عليه وسلم لَمَّا فَتَحَ حُنَيْنًا قَسَمَ الْغَنَائِمَ فَأَعْطَى الْمُؤَلَّفَةَ قُلُوبُهُمْ فَبَلَغَهُ أَنَّ الأَنْصَارَ يُحِبُّونَ أَنْ يُصِيبُوا مَا أَصَابَ النَّاسُ فَقَامَ رَسُولُ اللَّهِ صلى الله عليه وسلم فَخَطَبَهُمْ فَحَمِدَ اللَّهَ وَأَثْنَى عَلَيْهِ ثُمَّ قَالَ
يَا مَعْشَرَ الأَنْصَارِ أَلَمْ أَجِدْكُمْ ضُلاَّلاً فَهَدَاكُمُ اللَّهُ بِي وَعَالَةً فَأَغْنَاكُمُ اللَّهُ بِي وَمُتَفَرِّقِينَ فَجَمَعَكُمُ اللَّهُ بِي
وَيَقُولُونَ اللَّهُ وَرَسُولُهُ أَمَنُّ. فَقَالَ
فَقَالُوا اللَّهُ وَرَسُولُهُ أَمَنُّ. فَقَالَ
أَمَا إِنَّكُمْ لَوْ شِئْتُمْ أَنْ تَقُولُوا كَذَا وَكَذَا وَكَانَ مِنَ الأَمْرِ كَذَا وَكَذَا
لأَشْيَاءَ عَدَّدَهَا. زَعَمَ عَمْرٌو أَنْ لاَ يَحْفَظُهَا فَقَالَ
أَلاَ تَرْضَوْنَ أَنْ يَذْهَبَ النَّاسُ بِالشَّاءِ وَالإِبِلِ وَتَذْهَبُونَ بِرَسُولِ اللَّهِ إِلَى رِحَالِكُمُ الأَنْصَارُ شِعَارٌ وَالنَّاسُ دِثَارٌ وَلَوْلاَ الْهِجْرَةُ لَكُنْتُ امْرَأً مِنَ الأَنْصَارِ وَلَوْ سَلَكَ النَّاسُ وَادِيًا وَشِعْبًا لَسَلَكْتُ وَادِيَ الأَنْصَارِ وَشِعْبَهُمْ إِنَّكُمْ سَتَلْقَوْنَ بَعْدِي أَثَرَةً فَاصْبِرُوا حَتَّى تَلْقَوْنِي عَلَى الْحَوْضِ
Abdullah ibn Zaid reported that when the Messenger of Allah (ﷺ) conquered Hunain he distributed the booty, and he bestowed upon those whose hearts it was intended to win. It was conveyed to him (the Holy Prophet) that the Ansar cherished a desire that they should be given (that very portion) which the people (of Quraish) had got. Upon this the Messenger of Allah (ﷺ) stood up and, after having praised Allah and lauded Him, addressed them thus:
O people of Ansar, did I not find you erring and Allah guided you aright through me, and (in the state of) being destitute and Allah made you free from want through me, and in a state of disunity and Allah united you through me
They (the Ansar) said: Allah and His Messenger are most benevolent. He (again) said:
Why do you not answer me?
They said: Allah and His Messenger are the most benevolent. He said:
If you wish you should say so and so, and the event (should take) such and such course (and in this connection, he made a mention) of so many things.
'Amr is under the impression that he has not been able to remember them. He (the Holy Prophet) further said:
Don't you feel happy (over this state of affairs) that the people should go away with goats and camels, and you go to your places along with the Messenger of Allah? The Ansar are inner garments (more close to me) and (other) people are outer garments. Had there not been migration, I would have been a man from among the Ansar. If the people were to tread a valley or a narrow path, I would tread the valley (chosen) by the Ansar or narrow path (trodden) by them. And you would soon find after me preferences (over you in getting material benefits). So you should show patience till you meet me at the Haud (Kauthar).
The conclusion is that the Prophet ﷺ after the Hijra considered himself one of al-Ansār due to their stance with him and their support during the time in Medina.