Start with this Du'a
Consoling the bereaved: to Allah (SWT) belongs what He took
إِنَّ للَّهِ مَا أَخَذَ، وَلَهُ مَا أَعْطَى، وَكُلُّ شَيْءٍ عِنْدَهُ بِأَجَلٍ مُسَمَّى... فَلْتَصْبِرْ وَلْتَحْتَسِبْ
Inna lillahi ma akhadh, wa lahu ma a'ta, wa kullu shay'in 'indahu bi-ajalin musamma... fal-tasbir wal-tahtasib
“To Allah belongs what He took, and to Him belongs what He gave, and everything with Him has an appointed term... so let her be patient and hope for Allah's reward.”
Sahih al-Bukhari 1284, Sahih Muslim 923
The Prophet ﷺ sent this to his daughter when her child was dying. Speaking to the bereaved directly, say fasbir wahtasib to a man or fasbiri wahtasibi to a woman. The wording A'dhama-llahu ajrak in Hisn al-Muslim is from scholars, not a hadith, and is left out.