This ḥadīth has a sound chain of transmission. As for Maslamah, he was a Companion, yet nothing of his is found in the canonical collections except what al-Sunan of Abū Dāwūd records from him on the authority of Rufaʿīʿ ibn Thābit.
With this same chain: Ibn Jumayʿ informed us; Jaʿfar ibn Muḥammad al-Hamadānī narrated to us; Hilāl ibn al-ʿAlāʾ narrated to us; Ḥajjāj ibn Muḥammad narrated to us; Ibn Jurayj narrated to us; he said, “Mūsā ibn ʿUqbah related to me, from Suhayl ibn Abī Ṣāliḥ, from his father, from Abū Hurayrah, that the Prophet ﷺ said:
‘Whoever sits in a gathering in which there is much idle talk, and before rising says: «Subḥānaka Rabbana wa bi-ḥamdik, lā ilāha illā Ant, astaghfiruka thumma atūbu ilayk»—“Glory be to You, our Lord, and praise is Yours; there is no deity but You; I seek Your forgiveness and then repent to You”—will be forgiven for whatever occurred during that gathering.’ (1)
This is a sound, singular ḥadīth.
Judge Tāj al-Dīn ʿAbd al-Bāqī’s History records that Ibn Jurayj came as a petitioner to Maʿn ibn Zāʾida because of a debt he had incurred, and he stayed with him until the tenth of Dhū al-Qaʿdah. On his way he passed by a group who were singing
= In the collection Kitab al-Dhikr (no. 2699), chapter: Gathering to recite the Quran.
Abu Dawud also records it under no. 4893, chapter: Brotherhood, and no. 4946, chapter: Assistance to a Muslim; he further relates an abridged version in the Book of Prayer, no. 1455. Al-Tirmidhi transmits it under no. 1425 in the Book of Hudud, chapter: Concealing a Muslim’s faults, under no. 1931 in al-Birr with the same heading, and under no. 2646 in the Book of Qiraʾat, chapter: The merit of studying the Quran together.
Ibn Majah includes it under no. 225 in the Introduction, chapter: The excellence of the scholars, and—in abbreviated form—under no. 2544 in Hudud, chapter: Concealing the believer. Al-Hafiz al-Munziri also attributes the narration to al-Nasaʾi.
(1) Its isnad is strong. Al-Tirmidhi records it as well under no. 3429 in the Book of Supplications, chapter: What a person says when leaving a gathering, and rates it hasan. Abu Dawud cites it under no. 4858 in the Book of Manners, chapter: Expiation for a gathering; Ibn Hibban authenticates it (no. 2366); al-Hakim (1/536) grades it sahih, and al-Dhahabi concurs. Their evaluation is sound.
Additional attestations on the same subject exist: from Abu Barza al-Aslami, recorded by Abu Dawud (no. 4859), al-Darimi (2/283), and al-Hakim (1/536–537); from Abd Allah b. Amr b. al-As, recorded by Abu Dawud (no. 4857) and declared sahih by Ibn Hibban (no. 2367); from Jubayr b. Mutʿim, recorded by al-Nasaʾi, al-Tabarani, and al-Hakim; from Rafiʿ b. Khadij, recorded by al-Nasaʾi and al-Hakim; and from Aʿishah, also recorded by al-Hakim.