The Prophet ﷺ said, "Allah has assigned an angel to the womb, who says, 'My Lord, a drop of fluid. My Lord, a clot. My Lord, a lump of flesh.' Then, when Allah wishes to complete its creation, the angel asks, 'Male or female? Wretched or blessed? What is the provision? What is the appointed term?' All of this is then written while the child is still in its mother's womb."
عن النبي ﷺ: إن الله عز وجل وكل بالرحم ملكا يقول: يا رب نطفة، يا رب علقة، يا رب مضغة، فإذا أراد خلقه قال الملك: أذكر أم أنثى؟ شقي أم سعيد؟ فما الرزق؟ فما يقضي الأجل؟ فيكتب في بطن أمه.
Bukhari narrated it in the Book of Menstruation (318), and Muslim in the Book of Divine Decree (2646), both from the hadith of Hammad bin Zayd, who said, Ubaydullah bin Abu Bakr narrated to us, from Anas bin Malik, and he mentioned the hadith, and their wordings are close to each other.