Sure Al-Anfaal

8:51

ذَٰلِكَ بِمَا قَدَّمَتْ أَيْدِيكُمْ وَأَنَّ ٱللَّهَ لَيْسَ بِظَلَّٰمٍۢ لِّلْعَبِيدِ

Dies (geschieht) wegen dessen, was eure Hände vorausgeschickt haben, und deshalb, weil Allah keiner ist, der den Menschen Unrecht zufügt."

Tafsir 8:51

The cause of that painful punishment at the time when your souls are taken out and the burning punishment in your graves and in the Afterlife is because of the evil actions you performed in the world. Allah does not wrong people, but passes judgement on them with fairness. He is not unjust to His slaves.