The verse that speaks of this is Qur'an 7:172, in which Allah took from the children of Adam, from their loins, their offspring and made them testify, asking: 'Am I not your Lord?' and they said: 'Yes, we testify.'
- Scholars hold two main views: one that it was a real, literal gathering of the souls before birth, supported by reports from Ibn 'Abbas and 'Umar (ra) and by Ibn Hazm and, in a form, by Ibn al-Qayyim; the other that it describes the natural disposition (fitra) to recognise Allah, held by al-Zamakhshari, al-Razi and, in his commentary, favoured by Ibn Kathir among others.
- The Qur'an itself gives the reason for the verse: so that people may not say on the Day of Judgement that they were unaware of this (7:172).
- None of the details of any gathering are in the Qur'an, so the unseen details must be taken with their label and no more: what is certain is the covenant, and what is debated is its manner.
Introduction
This question concerns the unseen, so every statement must carry its text and its label. The Qur'an mentions a covenant that Allah took from the descendants of Adam (as). The nature of that event is where scholars differ, and this answer gives each view with its evidence and the scholars who hold it, and does not add to what the texts say.
There is nothing to be gained in presenting more than is known. A student of Islam should keep the certain apart from the debated: that Allah made a covenant with humanity is the Qur'an's statement, while whether it was a gathering of souls, a mental scene or the planting of the fitra is the work of interpretation.
The text of the Qur'an
The verse is in Surat al-A'raf:
وَإِذْ أَخَذَ رَبُّكَ مِن بَنِي آدَمَ مِن ظُهُورِهِمْ ذُرِّيَّتَهُمْ وَأَشْهَدَهُمْ عَلَىٰ أَنفُسِهِمْ أَلَسْتُ بِرَبِّكُمْ ۖ قَالُوا بَلَىٰ
"And when your Lord took from the children of Adam, from their loins, their descendants and made them testify about themselves: 'Am I not your Lord?' They said: 'Yes, we testify.'" (7:172)
Two features of the wording are important to scholars. The verse says that the offspring were taken from the loins of the children of Adam, and not from the back of Adam himself, and it gives a reason in the next verse: so that you do not say on the Day of Resurrection that we were unaware of this, or say that our forefathers associated partners and we were their offspring (7:172 to 7:173). The reason shows that the point of the covenant is to remove the excuse of heedlessness.
The view that it was a literal gathering
Those who hold that the souls were gathered rely on hadith. Imam Malik in al-Muwatta', Abu Dawud and al-Tirmidhi record a report from 'Umar ibn al-Khattab (ra) that the Prophet (saw) was asked about the verse and said that Allah created Adam, stroked his back with His right hand and brought out from him offspring, saying that He created these for Paradise, and then stroked his back again and brought out offspring who were for the Fire. Al-Tirmidhi called it hasan, though some scholars point out that one narrator in the chain, Muslim ibn Yasar, is little known, so the report has been debated. Another report in Musnad Ahmad, from Ibn 'Abbas as his own statement (mawquf), says that the covenant was taken at Na'man, near 'Arafat, and that Allah brought out all offspring as small particles and spoke to them. Some scholars graded this reliable, and others held that it is the opinion of a Companion and is a matter of the unseen.
Among scholars, Ibn Hazm in al-Fisal held that souls were created before bodies. Ibn al-Qayyim, in Kitab al-Ruh, discussed the reports in detail and set out the arguments of both sides, and leaned to treating the covenant as a real event. A supporting hadith in Sahih al-Bukhari and Sahih Muslim says that souls are like drafted armies, and those that know each other become friendly, and those that do not differ. Some scholars cite this for the pre-existence of souls, though others read it as a statement about the nature of souls.
The view that it describes the fitra
The second view is that no scene took place, and the verse describes how Allah created human nature with the recognition of Him inside it. It is held by al-Zamakhshari in al-Kashshaf, and by Fakhr al-Din al-Razi in al-Tafsir al-Kabir. Ibn Kathir in his commentary reports the traditions of the first view, but after mentioning them says that the verse shows that Allah made them testify by the fitra, and this is the view that the words of the verse fit best. Many contemporary scholars, among them Muhammad Rashid Rida in Tafsir al-Manar and Ibn 'Ashur in al-Tahrir wa al-Tanwir, also incline to a figurative reading.
Their evidence is the verse itself, which says nothing about a scene, and the hadith of the fitra in Sahih al-Bukhari and Sahih Muslim: every child is born upon the fitra, and then his parents make him Jewish, Christian or Magian. The Qur'an says: 'So set your face to the religion, upright, the fitra of Allah upon which He created people' (30:30). On this reading the answer 'Yes' is the answer of the nature that Allah created in us. The reports about the stroking of the back, on this view, are accepted only as far as their chains allow, and in the sense of a teaching picture.
What follows and what does not
Both views agree that human beings carry a built-in knowledge of their Lord and that this removes the excuse of heedlessness. Neither view says that a person remembers the event, and the Qur'an does not claim that anyone recalls it. The scholars warn against building further claims on it, such as the exact place or number of souls, or the idea that every soul knew its future life in detail. The latter would conflict with Qur'an 17:15, which ties punishment to the coming of a messenger.
A practical lesson is that the call of the prophets is a reminder (dhikra) of what is already known, as the Qur'an says: 'Remind, for the reminder benefits the believers' (51:55). This is why many people on being told about Allah feel that they had always known it.
Conclusion
The Qur'an affirms that Allah took a covenant from the offspring of Adam and made them testify that He is their Lord. Whether this was a literal gathering of souls, as reports from 'Umar and Ibn 'Abbas suggest and Ibn Hazm and Ibn al-Qayyim discuss, or the creation of the fitra, as al-Zamakhshari, al-Razi and many commentators hold, is a point on which sincere scholars differ, and the texts do not settle it with a clear and agreed report. What is firm is the lesson, which is that the knowledge of Allah is in the nature of every person, so that no one may claim to have been unaware.
A student should hold the certain texts firmly, hold the debated details lightly and ask Allah for steadfastness.
References
Qur'an 7:172 to 7:173, 17:15, 30:30, 51:55
Sahih al-Bukhari and Sahih Muslim, the hadith of the fitra and the hadith of souls as drafted armies
Imam Malik, al-Muwatta'; Sunan Abi Dawud and Jami' al-Tirmidhi (the hadith of 'Umar, graded hasan by al-Tirmidhi, with a debated chain); Musnad Ahmad (the report of Ibn 'Abbas)
Tafsir Ibn Kathir; al-Zamakhshari, al-Kashshaf; al-Razi, al-Tafsir al-Kabir; Rashid Rida, Tafsir al-Manar; Ibn 'Ashur, al-Tahrir wa al-Tanwir
Ibn Hazm, al-Fisal fi al-Milal; Ibn al-Qayyim, Kitab al-Ruh