I asked, or the Messenger of Allah ﷺ was asked, "Which sin is greatest in the sight of Allah?"He replied, "That you set up a rival to Allah, though it is He who created you."I asked, "And then which?"He said, "That you kill your own child for fear that he will eat along with you."I asked, "And then which?"He said, "That you commit adultery with your neighbor's wife."The narrator said that this verse was then revealed, confirming what the Messenger of Allah ﷺ had said, "And those who do not call upon another god besides Allah, nor kill the soul which Allah has forbidden except by right, nor commit adultery" [Furqan 25:68].
سألت أو سئل رسول الله ﷺ: أي الذنب عند الله أكبر؟ قال: أن تجعل لله ندا وهو خلقك. قلت: ثم أي؟ قال: ثم أن تقتل ولدك خشية أن يطعم معك. قلت: ثم أي؟ قال: أن تزاني بحليلة جارك. قال: ونزلت هذه الآية تصديقالقول رسول الله ﷺ: {والذين لا يدعون مع الله إلها واخر ولا يقتلون النفس التي حرم الله إلا بالحق ولا يزنون}
Bukhari related it in the Tafsir (4761), and Muslim in the Faith (86), both of them through Abu Waail, from Abu Maysara Amr bin Shurahbil, from Abdullah bin Masud who said, and then he mentioned it. The wording given is that of Bukhari, and the wording of Muslim is similar.