Caution: Translations of Quran and Ḥadīth may lead to possible misapplications and misinterpretations. This site is intended for students of sacred knowledge that are proficient in comprehending classical Arabic and have a strong foundation in Islamic sciences. Also note that religious injunctions rely on several aspects beyond what one may glean through reading individual aḥādīth.
quran:24:3

The fornicator does not marry except a ˹female˺ fornicator or polytheist, and none marries her except a fornicator or a polytheist, and that has been made unlawful to the believers.  

The fornicator shall not marry anyone but a fornicatress or an idolatress, and the fornicatress shall be married by none except a fornicator or an idolator, in other words, what is suitable for each of the two ˹for the purposes of marriage˺ is the above-mentioned; and that, namely, marrying of fornicators, is forbidden to believers, who are better ˹than those˺. This was revealed when the poor among the Emigrants resolved to marry the wealthy whores of the idolaters, so that they ˹the women˺ would provide for them. Thus it is said that the prohibition applies specifically to them; but it is also said to apply in general; but it was abrogated by God’s words: Marry off the spouseless among you ˹Q. 24:32˺.
القرآن:٢٤:٣

الزَّانِي لَا يَنْكِحُ إِلَّا زَانِيَةً أَوْ مُشْرِكَةً وَالزَّانِيَةُ لَا يَنْكِحُهَا إِلَّا زَانٍ أَوْ مُشْرِكٌ ۚ وَحُرِّمَ ذَٰلِكَ عَلَى الْمُؤْمِنِينَ  

{الزاني لا ينكح} يتزوج {إلا زانية أو مشركة والزانية لا ينكحها إلا زان أو مشرك} أي المناسب لكل منهما ما ذكر {وحرم ذلك} أي نكاح الزواني {على المؤمنين} الأخيار، نزل ذلك لما همَّ فقراء المهاجرين أن يتزوجوا بغيا المشركين وهن موسرات لينفقن عليهم فقيل التحريم خاص بهم وقيل عام ونسخ بقوله تعالى (وأنكحوا الأيامى منكم).

See similar narrations below:

Collected by Ḥākim, Bayhaqī
hakim:2701Abū Bakr b. Isḥāq > Abū al-Muthanná > Musaddad > Yaḥyá b. Saʿīd > ʿUbaydullāh b. al-Akhnas > ʿAmr b. Shuʿayb from his father from his grandfather

[Machine] "I came to the Prophet ﷺ and said, 'O Messenger of Allah, can we marry slaves?' He remained silent, and then I recited the verse from the Quran: 'The adulterer does not marry except an adulteress or polytheist, and none marries her except an adulterer or a polytheist. And that has been made unlawful to the believers' (Quran 24:3). So the Messenger of Allah ﷺ read it to me and said, 'Do not marry her.'"  

الحاكم:٢٧٠١حَدَّثَنَا أَبُو بَكْرِ بْنُ إِسْحَاقَ أَنْبَأَ أَبُو الْمُثَنَّى ثنا مُسَدَّدٌ ثنا يَحْيَى بْنُ سَعِيدٍ حَدَّثَنِي عُبَيْدُ اللَّهِ بْنُ الْأَخْنَسِ عَنْ عَمْرِو بْنِ شُعَيْبٍ عَنْ أَبِيهِ عَنْ جَدِّهِ أَنَّ مَرْثَدَ بْنَ أَبِي مَرْثَدٍ الْغَنَوِيَّ كَانَ يَحْمِلُ الْأُسَارَى بِمَكَّةَ وَكَانَ بِمَكَّةَ بَغِيٌّ يُقَالُ لَهَا عَنَاقُ وَكَانَتْ صَدِيقَتَهُ قَالَ

فَجِئْتُ إِلَى النَّبِيِّ ﷺ فَقُلْتُ يَا رَسُولَ اللَّهِ أَنْكِحُ عَنَاقَ؟ قَالَ فَسَكَتَ عَنِّي فَنَزَلَتْ {الزَّانِي لَا يَنْكِحُ إِلَّا زَانِيَةً أَوْ مُشْرِكَةً وَالزَّانِيَةُ لَا يَنْكِحُهَا إِلَّا زَانٍ أَوْ مُشْرِكٌ وَحُرِّمَ ذَلِكَ عَلَى الْمُؤْمِنِينَ} [النور 3] فَقَرَأَ عَلَيَّ رَسُولُ اللَّهِ ﷺ وَقَالَ «لَا تَنْكِحْهَا»  

هَذَا حَدِيثٌ صَحِيحُ الْإِسْنَادِ وَلَمْ يُخَرِّجَاهُ صحيح
bayhaqi:13863Abū ʿAbdullāh al-Ḥāfiẓ And ʿUbayd > Abū al-ʿAbbās > Yaḥyá > ʿAbd al-Wahhāb > Saʿīd > Qatādah > Saʿīd b. Jubayr

[Machine] He said about this verse {The adulterer does not marry except an adulteress or polytheist, and none marries her except an adulterer or a polytheist} [An-Nur 3], he said: There were known prostitutes in the city, so Allah made their marriage forbidden for the believers, and this is the statement of Qatadah.  

البيهقي:١٣٨٦٣وَأَخْبَرَنَا أَبُو عَبْدِ اللهِ الْحَافِظُ وَعُبَيْدٌ قَالَا ثنا أَبُو الْعَبَّاسِ ثنا يَحْيَى أنبأ عَبْدُ الْوَهَّابِ أنبأ سَعِيدٌ عَنْ قَتَادَةَ عَنْ سَعِيدِ بْنِ جُبَيْرٍ

أَنَّهُ قَالَ فِي هَذِهِ الْآيَةِ {الزَّانِي لَا يَنْكِحُ إِلَّا زَانِيَةً أَوْ مُشْرِكَةً وَالزَّانِيَةُ لَا يَنْكِحُهَا إِلَّا زَانٍ أَوْ مُشْرِكٌ} [النور 3] قَالَ كُنَّ بَغَايَا فِي الْمَدِينَةِ مَعْلُومٌ شَأْنُهُنَّ فَحَرَّمَ اللهُ نِكَاحَهُنَّ عَلَى الْمُؤْمِنِينَ وَهُوَ قَوْلُ قَتَادَةَ