Praise be to Allah, and may peace and blessings be upon the Messenger of Allah. To proceed:
There is no contradiction between this hadith and the eternal punishment of the disbelievers in Hell, because those whom Allah brings out of Hell in this hadith are not disbelievers who died upon shirk. Rather, they are people of Tawhid and faith, although they were extremely negligent regarding righteous deeds and acts of obedience.
First: A disbeliever who dies upon shirk will not be taken out of Hell
The Qur’an establishes that whoever dies associating partners with Allah will not be forgiven. Allah says:
“Indeed, Allah does not forgive associating others with Him, but He forgives anything less than that for whom He wills.”
[An-Nisa’ 4:48]
The Prophet ﷺ said:
“Whoever dies while associating anything with Allah will enter the Fire.”
Narrated by al-Bukhari and Muslim.
Therefore, the hadith about people being brought out of Hell cannot refer to those who died upon disbelief and shirk.
Second: Who are those about whom it is said, “They never did any good deed”?
They are people who possess the foundation of Tawhid and faith in Allah, but they did not perform sufficient righteous deeds to save themselves from punishment.
In the hadith of Abu Sa‘id al-Khudri (may Allah be pleased with him), intercession takes place for the people of faith. Then, after the intercession of the angels, prophets, and believers, Allah says:
“Then He will take a handful from the Fire and bring out from it people who never did any good deed…”
Narrated by Muslim.
Allah will then admit them into Paradise by His mercy.
Ibn Hajar (may Allah have mercy on him) explained that this intercession applies to those who said, “There is no god worthy of worship except Allah,” yet did no good deeds.
Thus, possessing the foundation of Tawhid is the essential distinction between them and the disbelievers.
Third: How can it be said that “they never did any good deed” if they had faith?
What is meant is that they did not perform outward righteous deeds sufficient to save them. It does not mean that they had no faith in Allah or Tawhid whatsoever.
Ibn ‘Abd al-Barr (may Allah have mercy on him) explained that the phrase “never did any good deed” does not include the foundation of Tawhid, because whoever dies as a disbeliever is not among those who receive forgiveness and are brought out of Hell.
For this reason, some narrations concerning the man who instructed his sons to burn his body after his death contain the wording:
“He never did any good deed except Tawhid.”
Thus, faith and Tawhid were present, even though the person was extremely negligent in righteous deeds.
Fourth: Does this hadith mean that righteous deeds are unimportant?
No.
The Qur’an and Sunnah are filled with commands to establish prayer, give Zakah, fast, and perform all other acts of obedience, as well as warnings against abandoning obligations and committing prohibited acts.
The hadith does not encourage abandoning righteous deeds. Rather, it demonstrates the vastness of Allah’s mercy toward the people of Tawhid, and that a sinful believer in Tawhid is not the same as a disbelieving polytheist.
Some sinful believers may enter Hell because of their sins and later be brought out by Allah’s mercy and the intercession of those permitted to intercede. But whoever dies upon major shirk will not benefit from this intercession.
Conclusion
People are not all the same in this matter:
Whoever dies upon major shirk: Allah will not forgive them if they die upon it, and their ultimate fate is eternal punishment in Hell.
Whoever dies upon Tawhid and faith while having sins and shortcomings: They are subject to Allah’s will. If He wills, He will forgive them, and if He wills, He will punish them, but they will not remain eternally in Hell as the disbelievers do.
Those whom the hadith describes as having “never done any good deed”: They are not disbelievers. Rather, they possessed the foundation of Tawhid and faith, and therefore Allah’s mercy encompassed them and He brought them out of Hell.
Thus, there is no contradiction between this hadith and the texts affirming that disbelievers will remain eternally in Hell.
And Allah knows best.