Sunnah.com reference : Book 7, Hadith 12English translation : Book 7, Hadith 793Arabic reference : Book 7, Hadith 790
Narrated 'Aishah (RA):
Barirah came to her and said, "I had arranged to buy my freedom for nine Uqiya; one Uqiyah to be paid annually, so help me." 'Aishah (RA) replied, "If your people are pleased that I should pay (the amount) to them, and I shall have the right to inherit from you, I shall do so." Barira went to her people and told them about it, but they refused the offer. When she came back Allah's Messenger (ﷺ) was sitting (in the house). She said, "I offered that to them, but they insisted that the right to inherit from me should be theirs." The Prophet (ﷺ) heard that and 'Aishah (RA) told him about it. So, he said to 'Aishah (RA), "Take her and stipulate that the right to inherit from her will be yours, for the right of inheritance from her will be yours, for the right of inheritance belongs only to the one who has set a slave free." 'Aishah (RA) did so. Allah's Messenger (ﷺ) then stood up among the people to address them. So, after praising and extolling Allah, he said, "To proceed; what is the matter with some men who make conditions which are not in the Book of Allah, the Might, the Majestic? Any condition which is not in Allah's Book is invalid. Even if there are a hundred conditions, Allah's Decision is more valid and Allah's Condition is more binding. The right of inheritance belongs only to the one who has set a slave free."

[Agreed upon and the wording is al-Bukhari's]. Muslim has: "Buy her, set her free and make the stipulation that the right to inherit from her will be yours."
وَعَنْ عَائِشَةَ ‏-رَضِيَ اَللَّهُ عَنْهَا‏- قَالَتْ: { جَاءَتْنِي بَرِيرَةُ, فَقَالَتْ: كَاتَبْتُ أَهْلِي عَلَى تِسْعٍ أُوَاقٍ, فِي كُلِّ عَامٍ أُوقِيَّةٌ, فَأَعِينِينِي.‏ فَقُلْتُ: إِنْ أَحَبَّ أَهْلُكِ أَنْ أَعُدَّهَا لَهُمْ وَيَكُونَ وَلَاؤُكِ لِي فَعَلْتُ, فَذَهَبَتْ بَرِيرَةُ إِلَى أَهْلِهَا.‏ فَقَالَتْ لَهُمْ; فَأَبَوْا عَلَيْهَا, فَجَاءَتْ مِنْ عِنْدِهِمْ, وَرَسُولُ اَللَّهِ ‏- صلى الله عليه وسلم ‏-جَالِسٌ.‏ فَقَالَتْ: إِنِّي قَدْ عَرَضْتُ ذَلِكَ عَلَيْهِمْ فَأَبَوْا إِلَّا أَنْ يَكُونَ اَلْوَلَاءُ لَهُمْ, فَسَمِعَ اَلنَّبِيُّ ‏- صلى الله عليه وسلم ‏-فَأَخْبَرَتْ عَائِشَةُ اَلنَّبِيَّ ‏- صلى الله عليه وسلم ‏-.‏ فَقَالَ: خُذِيهَا وَاشْتَرِطِي لَهُمُ اَلْوَلَاءَ, فَإِنَّمَا اَلْوَلَاءُ لِمَنْ أَعْتَقَ فَفَعَلَتْ عَائِشَةُ, ثُمَّ قَامَ رَسُولُ اَللَّهِ ‏- صلى الله عليه وسلم ‏-فِي اَلنَّاسِ [ خَطِيباً ], فَحَمِدَ اَللَّهَ وَأَثْنَى عَلَيْهِ.‏ ثُمَّ قَالَ: 
" أَمَّا بَعْدُ, مَا بَالُ رِجَالٍ يَشْتَرِطُونَ شُرُوطاً لَيْسَتْ فِي كِتَابِ اَللَّهِ عَزَّ وَجَلَّ مَا كَانَ مِنْ شَرْطٍ لَيْسَ فِي كِتَابِ اَللَّهِ فَهُوَ بَاطِلٌ, وَإِنْ كَانَ مِائَةَ شَرْطٍ, قَضَاءُ اَللَّهِ أَحَقُّ, وَشَرْطُ اَللَّهِ أَوْثَقُ, وَإِنَّمَا اَلْوَلَاءُ لِمَنْ أَعْتَقَ " }  مُتَّفَقٌ عَلَيْهِ, وَاللَّفْظُ لِلْبُخَارِيِّ  1‏ .‏ 
وَعِنْدَ مُسْلِمٍ فَقَالَ: { اِشْتَرِيهَا وَأَعْتِقِيهَا وَاشْتَرِطِي لَهُمُ اَلْوَلَاءَ }‏1 ‏- صحيح.‏ رواه البخاري ( 2168 )‏، ومسلم ( 1504 )‏.‏