Someone said, "I asked Mujahid about the sajdah in Surah Sad."Mujahid said, "I asked Ibn Abbas, 'Where did you derive this prostration from?'Ibn Abbas replied, "Do you not read 'And of his descendants were Dawud and Sulayman' [Anam 6:84], and 'Those are the ones whom Allah has guided, so follow their guidance' [Anam 6:90]? Dawud was among those whom your Prophet ﷺ was commanded to follow. Dawud performed that prostration, so the Messenger of Allah ﷺ performed it as well."
سألت مجاهدا عن سجدة في ص فقال: سألت ابن عباس من أين سجدت؟ فقال: أو ما تقرأ {ومن ذريته داوود وسليمان} {أوليك الذين هدى الله فبهداهم اقتدة} فكان داود ممن أمر نبيكم ﷺ أن يقتدي به، فسجدها داود عليه السلام فسجدها رسول الله ﷺ.
Bukhari narrated it in the tafsir (4807), from Muhammad bin Abdullah, Muhammad bin Ubayd Tanafisi narrated to us, from Awwam, who said, and he mentioned it.I say, the verse during which the Prophet ﷺ prostrated in Surah Sad is His saying, Most High, "And Dawud was certain that We had tried him, so he sought forgiveness of his Lord and fell down bowing, and turned in repentance" [Sad 38:24].