The Buddha explains how Moggallāna became so mighty and powerful through the development and frequent practice of the four bases of psychic ability.

Moggallāna sutta - Moggallāna

Then the Blessed One addressed the bhikkhus: "What do you think, bhikkhus? Through the development and frequent practice of what qualities has the bhikkhu Moggallāna become so mighty and powerful?"

"Venerable sir, our teachings are rooted in the Blessed One, guided by the Blessed One, and have the Blessed One as their refuge. It would be good, venerable sir, if the Blessed One himself were to explain the meaning of this. Having heard it from the Blessed One, the bhikkhus will remember it."

"Bhikkhus, it is through the development and frequent practice of the four bases of psychic ability that the bhikkhu Moggallāna has become so mighty and powerful.

And what are these four? Here, bhikkhus, the bhikkhu Moggallāna develops the basis of psychic ability that is endowed with collectedness arising from aspiration and accompanied by intentional effort thus his aspiration is neither too slack nor too tense, it is not inwardly inhibited nor outwardly scattered. He dwells continuously aware as before, so after; as after, so before; as below, so above; as above, so below; as by day, so by night; as by night, so by day. In this way, with an open and unenveloped mind, he develops a mind that is radiant.

He develops the basis of psychic ability that is endowed with collectedness arising from energy and accompanied by intentional effort thus his energy is neither too slack nor too tense, it is not inwardly inhibited nor outwardly scattered. He dwells continuously aware as before, so after; as after, so before; as below, so above; as above, so below; as by day, so by night; as by night, so by day. In this way, with an open and unenveloped mind, he develops a mind that is radiant.

He develops the basis of psychic ability that is endowed with collectedness arising from [purification of] mind and accompanied by intentional effort thus his mind is neither too slack nor too tense, it is not inwardly inhibited nor outwardly scattered. He dwells continuously aware as before, so after; as after, so before; as below, so above; as above, so below; as by day, so by night; as by night, so by day. In this way, with an open and unenveloped mind, he develops a mind that is radiant.

He develops the basis of psychic ability that is endowed with collectedness arising from investigation and accompanied by intentional effort thus his investigation is neither too slack nor too tense, it is not inwardly inhibited nor outwardly scattered. He dwells continuously aware as before, so after; as after, so before; as below, so above; as above, so below; as by day, so by night; as by night, so by day. In this way, with an open and unenveloped mind, he develops a mind that is radiant.

Bhikkhus, through the development and frequent practice of these four bases of psychic ability, the bhikkhu Moggallāna has become so mighty and powerful.

Furthermore, bhikkhus, through the development and frequent practice of these four bases of psychic ability, the bhikkhu Moggallāna experiences various kinds of psychic abilities being one, he becomes many; being many, he becomes one; he appears and disappears; he passes through walls, enclosures, and mountains as though through space; he dives in and out of the earth as though it were water; he walks on water without sinking as though on solid ground; he flies through the air cross-legged like a bird with wings; he touches and strokes the sun and moon, so mighty and powerful; and he controls his body as far as the Brahmā world.

Moreover, bhikkhus, through the development and frequent practice of these four bases of psychic ability, the bhikkhu Moggallāna, with the wearing away of the taints, realized for himself with direct knowing, in this very life, the taintless liberation of mind and liberation by wisdom, and having entered upon it, dwells in it."