The Buddha achieved enlightenment many, many, many lifetimes before he was ever born as Siddhartha Gautama. The story of his enlightenment was a useful and skillful retelling of how he became enlightened long, long, long before. Why did the Buddha manifest as an unenlightened young Bodhisattva named Gautama who completed the path in this very life? He did it out of compassion for all sentient beings to teach them the steps to liberation.
This is a point of contention between the Theravada and Mahayana view of the Buddha. According to the latter he was already enlightened aeons before he ever manifested as Shakyamuni. See here for contemporary account from the Mahayana viewpoint.
For a Mahayana sutra reference have a look at the Lotus Sutra.
This is from a recent english translation of the Lotus Sutra. Listen to how Bodhisattva Maitreya implores Shakyamuni to explain how Shakyamuni describes enlightened deeds committed aeons ago juxtaposed with his recent enlightenment 40 years ago:
Then Bodhisattva Mahāsattva Maitreya and the innumerable other
bodhisattvas became doubtful and confused concerning this
unprecedented experience. They thought this:
How is it possible in such a short time for the Bhagavat to have
inspired such an immeasurable, limitless, incalculable number of great
bodhisattvas, enabling them to abide in highest, complete
enlightenment? Immediately they addressed the Buddha, saying: “O
Bhagavat! When the Tathāgata was a prince he left the palace of the
Śākyas, sat on the terrace of enlightenment which is not far from the
city of Gayā, and attained highest, complete enlightenment. Since then
more than forty years have passed. How is it possible, O Bhagavat, for
you to have done such great buddha acts in such a short period of
time? Is it through the might of the Buddha and through the Buddha’s
qualities that you have inspired such an assembly of incalculable
great bodhisattvas to achieve highest, complete enlightenment?
...
This would be difficult to believe; and what the Buddha has now taught
is exactly like this. It has not, in fact, been so long since the
Buddha attained the path. Yet for the sake of the buddha path this
great assembly of bodhisattvas has been diligently striving for
innumerable thousands of myriads of koṭis of kalpas.
...
However, if the bodhisattvas in whom the thought of enlightenment has
recently awakened hear this after the Buddha’s parinirvāṇa, they will
not accept it; and this will bring about conditions for erring deeds
that destroy the Dharma. That is why, O Bhagavat, we entreat you to
explain it to us and remove our doubts.
And this is how Buddha Shakyamuni answered:
Thereupon the Buddha addressed the bodhisattvas and the entire great
assembly, saying: “O sons of a virtuous family! You should believe the
true words of the Tathāgata.” He addressed the great assembly again,
saying: “You should believe the Tathāgata’s true words.” He repeated
this to them, saying: “You should believe the Tathāgata’s true words.”
Then the great assembly of bodhisattvas, headed by Maitreya, addressed
the Buddha with their palms pressed together, saying: “O Bhagavat! We
entreat you to explain it. We will accept the Buddha’s words.” After
they had spoken in this way three times, they again said: “We entreat
you to explain it. We will accept the Buddha’s words.”
After they had spoken in this way three times, they again said: “We
entreat you to explain it. We will accept the Buddha’s words.” Then
the Bhagavat, realizing that the bodhisattvas continued to entreat him
after those three times, addressed them, saying: “Listen carefully to
the Tathāgata’s secret and transcendent powers. The devas, humans, and
asuras in all the worlds all think that the present Buddha, Śākyamuni,
left the palace of the Śākyas, sat on the terrace of enlightenment not
far from the city of Gayā, and attained highest, complete
enlightenment. However, O sons of a virtuous family, immeasurable,
limitless, hundreds of thousands of myriads of koṭis of nayutas of
kalpas have passed since I actually attained buddhahood.
Why did Buddha Shakyamuni manifest himself as attaining enlightenment in 40 years in his own lifetime? Why does he tell some sentient beings that he will pass into paranirvana and be utterly extinguished when in fact he will not? ... skillful means:
“O sons of a virtuous family! If any sentient being comes to me, I
perceive the dullness or sharpness of his faith and other faculties
with my buddhaeye. According to the way I should bring them to the
path, I, myself, proclaim different names and lifespans in various
places. In each case I have also clearly stated that I would enter
parinirvāṇa. Through various skillful means I have explained
subtle teachings and have made the sentient beings rejoice.
“O sons of a virtuous family! To those beings whom the Tathāgata
perceives as taking pleasure in the inferior teachings, who have few
qualities and grave defilements, he teaches that the Buddha attained
highest, complete enlightenment after he renounced household life in
his young age. However, it has been a very long time indeed since I
attained buddhahood. I give such an explanation only to lead and
inspire the sentient beings to enter the buddha path through skillful
means.
Don't think for even a moment that the Buddha has spoken falsely. Everything he does is out of compassion for sentient beings. If your house was on fire and your kids refusing to budge you would use skillful means to lure them out of compassion. This would be a skillful and completely virtuous act without even an ounce of negativity. Listen to Shakyamuni:
“O sons of a virtuous family! The sutras that the Tathāgata has
expounded are all to save the sentient beings. Whether the Tathāgata
teaches about himself or others, whether he reveals his form or that
of others, whether he shows his acts or those of others, everything he
says is true, never false. “Why is this? Because the Tathāgata
perceives all the marks of the triple world as they really are: that
there is no birth and death, coming or going; that there is also no
existence or extinction in the world, truth or falsehood, sameness or
difference. The Tathāgata does not view the triple world as sentient
beings in the triple world see it. The Tathāgata perceives such things
clearly and without mistakes.
“Since sentient beings have various natures, desires, behaviors,
thoughts, and distinctions, the Tathāgata, wanting to cause them to
plant roots of good merit, has explained various teachings through a
variety of examples, explanations, and illustrations. He has not
desisted from doing buddha acts even for a single moment and in this
way it has been an extremely long time since I attained buddhahood. My
lifespan is immeasurable and incalculable. I abide forever without
entering parinirvāṇa.
All emphasis is my own. Please read the Lotus Sutra and understand.
Hope this helps!