'Abdullah b. Buraida reported on the authority of his father that Ma'iz b. Malik al-Aslami came to Allah's Messenger (ﷺ) and said:

Allah's Messenger, I have wronged myself; I have committed adultery and I earnestly desire that you should purify me. He turned him away. On the following day, he (Ma'iz) again came to him and said: Allah's Messenger, I have committed adultery. Allah's Messenger (ﷺ) turned him away for the second time, and sent him to his people saying: Do you know if there is anything wrong with his mind. They denied of any such thing in him and said: We do not know him but as a wise good man among us, so far as we can judge. He (Ma'iz) came for the third time, and he (the Holy Prophet) sent him as he had done before. He asked about him and they informed him that there was nothing wrong with him or with his mind. When it was the fourth time, a ditch was dug for him and he (the Holy Prophet) pronounced judg- ment about him and he wis stoned. He (the narrator) said: There came to him (the Holy Prophet) a woman from Ghamid and said: Allah's Messenger, I have committed adultery, so purify me. He (the Holy Prophet) turned her away. On the following day she said: Allah's Messenger, Why do you turn me away? Perhaps, you turn me away as you turned away Ma'iz. By Allah, I have become pregnant. He said: Well, if you insist upon it, then go away until you give birth to (the child). When she was delivered she came with the child (wrapped) in a rag and said: Here is the child whom I have given birth to. He said: Go away and suckle him until you wean him. When she had weaned him, she came to him (the Holy Prophet) with the child who was holding a piece of bread in his hand. She said: Allah's Apostle, here is he as I have weaned him and he eats food. He (the Holy Prophet) entrusted the child to one of the Muslims and then pronounced punishment. And she was put in a ditch up to her chest and he commanded people and they stoned her. Khalid b Walid came forward with a stone which he flung at her head and there spurted blood on the face of Khalid and so he abused her. Allah's Apostle (ﷺ) heard his (Khalid's) curse that he had huried upon her. Thereupon he (the Holy Prophet) said: Khalid, be gentle. By Him in Whose Hand is my life, she has made such a repentance that even if a wrongful tax-collector were to repent, he would have been forgiven. Then giving command regarding her, he prayed over her and she was buried.
وَحَدَّثَنَا أَبُو بَكْرِ بْنُ أَبِي شَيْبَةَ، حَدَّثَنَا عَبْدُ اللَّهِ بْنُ نُمَيْرٍ، ح وَحَدَّثَنَا مُحَمَّدُ بْنُ عَبْدِ، اللَّهِ بْنِ نُمَيْرٍ - وَتَقَارَبَا فِي لَفْظِ الْحَدِيثِ - حَدَّثَنَا أَبِي، حَدَّثَنَا بَشِيرُ بْنُ الْمُهَاجِرِ، حَدَّثَنَا عَبْدُ اللَّهِ بْنُ بُرَيْدَةَ، عَنْ أَبِيهِ، أَنَّ مَاعِزَ بْنَ مَالِكٍ الأَسْلَمِيَّ، أَتَى رَسُولَ اللَّهِ صلى الله عليه وسلم فَقَالَ يَا رَسُولَ اللَّهِ إِنِّي قَدْ ظَلَمْتُ نَفْسِي وَزَنَيْتُ وَإِنِّي أُرِيدُ أَنْ تُطَهِّرَنِي ‏.‏ فَرَدَّهُ فَلَمَّا كَانَ مِنَ الْغَدِ أَتَاهُ فَقَالَ يَا رَسُولَ اللَّهِ إِنِّي قَدْ زَنَيْتُ ‏.‏ فَرَدَّهُ الثَّانِيَةَ فَأَرْسَلَ رَسُولُ اللَّهِ صلى الله عليه وسلم إِلَى قَوْمِهِ فَقَالَ ‏"‏ أَتَعْلَمُونَ بِعَقْلِهِ بَأْسًا تُنْكِرُونَ مِنْهُ شَيْئًا ‏"‏ ‏.‏ فَقَالُوا مَا نَعْلَمُهُ إِلاَّ وَفِيَّ الْعَقْلِ مِنْ صَالِحِينَا فِيمَا نُرَى فَأَتَاهُ الثَّالِثَةَ فَأَرْسَلَ إِلَيْهِمْ أَيْضًا فَسَأَلَ عَنْهُ فَأَخْبَرُوهُ أَنَّهُ لاَ بَأْسَ بِهِ وَلاَ بِعَقْلِهِ فَلَمَّا كَانَ الرَّابِعَةَ حَفَرَ لَهُ حُفْرَةً ثُمَّ أَمَرَ بِهِ فَرُجِمَ ‏.‏ قَالَ فَجَاءَتِ الْغَامِدِيَّةُ فَقَالَتْ يَا رَسُولَ اللَّهِ إِنِّي قَدْ زَنَيْتُ فَطَهِّرْنِي ‏.‏ وَإِنَّهُ رَدَّهَا فَلَمَّا كَانَ الْغَدُ قَالَتْ يَا رَسُولَ اللَّهِ لِمَ تَرُدُّنِي لَعَلَّكَ أَنْ تَرُدَّنِي كَمَا رَدَدْتَ مَاعِزًا فَوَاللَّهِ إِنِّي لَحُبْلَى ‏.‏ قَالَ ‏"‏ إِمَّا لاَ فَاذْهَبِي حَتَّى تَلِدِي ‏"‏ ‏.‏ فَلَمَّا وَلَدَتْ أَتَتْهُ بِالصَّبِيِّ فِي خِرْقَةٍ قَالَتْ هَذَا قَدْ وَلَدْتُهُ ‏.‏ قَالَ ‏"‏ اذْهَبِي فَأَرْضِعِيهِ حَتَّى تَفْطِمِيهِ ‏"‏ ‏.‏ فَلَمَّا فَطَمَتْهُ أَتَتْهُ بِالصَّبِيِّ فِي يَدِهِ كِسْرَةُ خُبْزٍ فَقَالَتْ هَذَا يَا نَبِيَّ اللَّهِ قَدْ فَطَمْتُهُ وَقَدْ أَكَلَ الطَّعَامَ ‏.‏ فَدَفَعَ الصَّبِيَّ إِلَى رَجُلٍ مِنَ الْمُسْلِمِينَ ثُمَّ أَمَرَ بِهَا فَحُفِرَ لَهَا إِلَى صَدْرِهَا وَأَمَرَ النَّاسَ فَرَجَمُوهَا فَيُقْبِلُ خَالِدُ بْنُ الْوَلِيدِ بِحَجَرٍ فَرَمَى رَأْسَهَا فَتَنَضَّحَ الدَّمُ عَلَى وَجْهِ خَالِدٍ فَسَبَّهَا فَسَمِعَ نَبِيُّ اللَّهِ صلى الله عليه وسلم سَبَّهُ إِيَّاهَا فَقَالَ ‏"‏ مَهْلاً يَا خَالِدُ فَوَالَّذِي نَفْسِي بِيَدِهِ لَقَدْ تَابَتْ تَوْبَةً لَوْ تَابَهَا صَاحِبُ مَكْسٍ لَغُفِرَ لَهُ ‏"‏ ‏.‏ ثُمَّ أَمَرَ بِهَا فَصَلَّى عَلَيْهَا وَدُفِنَتْ ‏.‏
Reference : Sahih Muslim 1695 b
In-book reference : Book 29, Hadith 35
USC-MSA web (English) reference : Book 17, Hadith 4206
  (deprecated numbering scheme)