What is the Islamic stance on punishment in graves? Does not this belief negate the idea of God being just?
The information regarding punishment in graves mainly appears in some AHadith ascribed to the Holy Prophet (sws). It needs to be appreciated that these traditions do not add to the beliefs. These only help explain the religious sources: the Holy Qur'an and the Sunnah. We need to study these AHadith in the light of the Holy Qur'an, which is the real yardstick in religious matters. I would like you to appreciate that in this context 'grave' does not stand for the 'ditch' in which we bury the dead rather it connotes the life after death and before the Judgment Day namely Barzakh. The Holy Qur'an refers to this life in various places (36:52, 40:45-6, 2:154, 9:101 for example).
The life of Barzakh is neither identical to the life in this world, nor is it like life in the Hereafter. This we cannot fully comprehend like most other matters regarding the life after death and the Hereafter. We only know that some of the individuals experience a different kind of life and are rewarded or punished by their Lord in Barzakh but we do not know in what form this reward or torture is executed. The Holy Qur'an itself has made it clear that we do not and cannot comprehend this phenomenon and it has not provided extensive information on the subject. It says:
And call not those who are slain in the way of Allah 'dead'. Nay, they are living, only on perceive not. (2:154)
It would, therefore, be wise to avoid imagining details of the matter and to be content with the information made available by the Almighty.
The Holy Qur'an informs us that those who live a life of submission to God and sacrifice their lives, necessarily merit success in the Hereafter and will be rewarded right after their death. It says:
Think not of those, who are slain in the way of Allah, as dead. Nay, they are living. With their Lord they have provision. (3:169)
The Holy Qur'an has only provided this brief information It says that they are not dead like others rather they are being rewarded. This treatment is exclusive for the martyrs, the Messengers, Prophets, and other pious people who lived lives of piety and righteousness.
A similar but contrasting fate as that of the Shuhada (witnesses to the truth) awaits those who were not only clear rejecters of what they understood to be the truth but lived a life which was evident of their hatred for it, as, for instance, the rejecters and enemies of the Messengers of Allah. These people, contrary to the Shuhada, are subjected to God's punishments immediately after their worldly deaths. The Qur'an says:
The Fire; they are exposed to it morning and evening; and on the day when the Hour comes [it will be said]: Cause Pharaoh's folk to enter the most awful doom. (40:46)
The fate of those whose status is not clear is left to be decided undecided till the Hereafter. It will be then when their status will be ascertained and rewards and punishments will be declared. Many verses of the Holy Qur'an indicate to this (36:52 for example).
Now remains the position of the Ahdith, which refer to the punishment in grave. As stated earlier these Ahdith must be analyzed in the light of the Holy Qur'an and should only be accepted if found in conformity with the Qur'anic information on the subject. A thorough analysis of these Ahdith shows that these refer specifically to the addressees of the Messenger of Allah and we have seen that the Holy Qur'an has justified their chastisement in the grave. It would not be possible to generalize this punishment and reward for it would be a clear contradiction to the Qur'anic assertion that the fate of people will be ascertained after the process of accountability in the Day of Judgment.