It is authenticated from Abu Huraira that the Prophet ﷺ said:
“Ya’jūj and Ma’jūj dig everyday through the barrier (see Qur’ān 18:94-98) until, when they can almost see the rays of the Sun, the one in charge of them says, ‘withdraw; we will continue digging tomorrow.’ Then Allāh restores the barrier back to its original state, stronger than ever.
“This will continue until, when their time comes to be released and Allāh wants to release them upon the World, they will dig until, when they can almost see the Sun’s rays, the one in charge of them will say, ‘withdraw; We will continue digging tomorrow, if Allāh (Exalted, be He) Wills,’ constraining it [to Allah’s Will]. Thus, they will return to it, and it will be as they left it. Then they will dig through it and invade society.
“So, they will drink the waters dry, and people will shelter themselves from them in their fortresses. Then they (Ya’juj and Ma’juj) will shoot arrows into the sky and they will return with blood on them. They will say, ‘we have subdued the people on Earth, and we have overcome the people in heaven.’ Then Allāh will make worms grow in the napes of their necks, which will kill them.”
The Messenger of Allāh ﷺ said, “By the One in Whose Hand is my soul, the Earth’s beasts will grow fat and grow full udders due to their flesh”.1
Shaykh Muḥammad Naṣir al-Dīn al-Albānī said regarding the hadith:
Ḥāfiẓ ibn Kathīr quoted Imām Aḥmad’s version of the Ḥadīth under his exegesis of the āyahs that mention the story of Dhū al-Qarnayn and his building of the barrier, and His Statements (Exalted, be He) regarding Ya’jūj and Ma’jūj, of which is “Thus, they were not able to scale it and they were not able to bore through it.”
Then he (ibn Kathīr) said, “its chain of transmission is good and strong, however, ascribing the ḥadīth’s text to the Prophet ﷺ poses a challenge because the āyah indicates that they were unable to climb it or bore through it due to its skillful construction, robustness, and durability.”
Yes, however, the verse does not in any way imply that they will never be able to. The āyah is talking about the past, and the ḥadīth is referring to the future to come. Thus, this is neither a contradiction nor an anomaly, rather the ḥadīth is perfectly in line with the Qur’ān, particularly His statement “Until, when Ya’jūj & Ma’jūj are released and they, from every high ground, descend.” [al-Anbiyā’: 96].
After writing this, I checked the story in his (ibn Kathīr’s) book “al-Bidāyah wa al-Nihāyah”, and found that he gave an explanation similar to the one I gave, though I have some criticisms that would make this discourse lengthy if we were to decide to discuss them here. Hence, whoever wishes to have a look at it should refer to (2/112).
Silsilah al-Aḥādīth al-Ṣaḥīḥah vol. 4 pp. 313-315
1 Reported by al-Tirmidhī, ibn Mājah, Aḥmad, ibn Ḥibbān as well as al-Ḥākim who said, “it is authentic according to the conditions of al-Bukhārī and Muslim”; al-Dhahabī agreed with him, and al-Albānī said, “the ḥadīth is authentic, as they have both stated.“