This is in the Quran, 18: 83-99, which mean, [all the interpretation is according to the interpreter Mohammed-Ali Hassan Al-Hilly:]
(83. The [Christians and Quraish tribesmen] ask you [Mohammed] concerning Alexander who is surnamed 'Zul-qarnain.'
Say: "I will rehearse to you something of his story and account."
84. We established him on earth [by making him king and [army] leader, and We gave to him all means [of victory in all his campaigns.]
85. And he followed the way.
86. [And he went westward in his march] until when he reached at sun-set [time];
he found the [sun] setting under the observation of a black woman, and he found with her a people;
We said: "O 'Zul-qarnain', you have either to punish [these people who worship the sun if they do not believe], or to show them kindness [if they believe.]"
87. [Alexander] said: "Whoever wrongs [himself by the association with God, and does not believe], him shall we punish [with beating and imprisoning in the life of the World]; then shall he be returned [following his death] to his Lord; and He shall punish him [in the Next Life] with ‘grievous and extraordinary’ punishment."
88. But whoever believes and works righteous [works], will have a fair reward [of the prosperity in Paradise, in the Next Life], and Our am’r [: Our angels] will take him to retribution mildly."
89. Then he set on, following the means [of victory which We provided him with.]
90. Until when he reached to the east, he found it rise upon a people for whom We made no shelter therefrom.
91. As such [did Alexander deal with them]; and – after trying him – We knew his [righteous intentions and conduct.]
92. Then he continued with his march and conquests.
93. Until when [Alexander] reached [the region] between the two dams, he found before the two [dams] a people who could not almost understand any word [of his language.]
94. They said: "O 'Zul-qarnain', behold, Gog and Magog are corruptors in the earth; so shall we assign to you a tribute, in recompense of your setting up a barrier between us and between them [lest the water come upon and drown us?]"
95. He said: "[The wealth], that God has established me in, is better [than your tribute], so assist me with a power [of your workers], and I shall set up a barrier between you and them."
96. "Bring to me pieces of iron" – till when he had leveled up [the gap] between the two mountain-sides, he said: "[Now,] blow!" – till, when he had made the [iron red like] fire, he said: "Bring me, to pour the molten copper on the [iron]."
97. And [Gog and Magog] were unable [then] to surmount it [in order to destroy it], nor were they able to dig through it.
[Then when the dam was accomplished:]
98. [Alexander] said: "This is a mercy from my Lord [to you.]
But when the promise of my Lord will come to pass [before Doomsday], he will make it devastated; for the promise of my Lord is ever true.")
See more of the explanation of these ayat:
Here is the recitation of these ayat: