We set out with the Messenger of Allah ﷺ on the Farewell Pilgrimage. The men said, "Messenger of Allah, celibacy has become very difficult for us."He said, "Then enjoy temporary marriage with these women."So we approached them, but they refused to marry us unless we set a fixed term between ourselves and them. The men mentioned this to the Prophet ﷺ, and he said, "Set a fixed term between yourselves and them."So I went out together with a cousin of mine. He had a cloak with him, and I had a cloak too, but his cloak was finer than mine, while I was younger than he was. We came upon a woman, and she said, "A cloak is like a cloak."So I married her and stayed with her that night. Then I went out in the morning, and there was the Messenger of Allah ﷺ standing between the Corner and the Door, saying, "People, I had previously permitted you to engage in temporary marriage. Know that Allah has now forbidden it until the Day of Resurrection. So whoever has any of these women with him, let him release her, and do not take back anything of what you have given them."
حدثنا أبو بكر بن أبي شيبة حدثنا عبدة بن سليمان عن عبد العزيز بن عمر
خرجنا مع رسول الله ﷺ في حجة الوداع فقالوا يا رسول الله إن العزبة قد اشتدت علينا قال فاستمتعوا من هذه النساء فأتيناهن فأبين أن ينكحننا إلا أن نجعل بيننا وبينهن أجلا فذكروا ذلك للنبي ﷺ فقال اجعلوا بينكم وبينهن أجلا فخرجت أنا وابن عم لي معه برد ومعي برد وبرده أجود من بردي وأنا أشب منه فأتينا على امرأة فقالت برد كبرد فتزوجتها فمكثت عندها تلك الليلة ثم غدوت ورسول الله ﷺ قائم بين الركن والباب وهو يقول أيها الناس إني قد كنت أذنت لكم في الاستمتاع ألا وإن الله قد حرمها إلى يوم القيامة فمن كان عنده منهن شيء فليخل سبيلها ولا تأخذوا مما آتيتموهن شيئا
Muslim reported it in the Book of Marriage, chapter on the marriage of mutah and explaining that it was permitted, then abrogated, then permitted, then abrogated, etc., hadith no. 21/1406, from Ibn Abu Shayba, in abridged form. He has other routes for it, and he did not mention the phrase "during the Farewell Pilgrimage." The correct view is that it was during the Conquest of Makkah, as stated in Sahih Muslim and elsewhere.