Throughout the books, there are many places where Rand fulfils the prophecies of the Karaethon Cycle by knowing it in advance
-
The Stone of Tear will never fall, till Callandor is wielded by the Dragon’s hand
Knowing this part, Rand sets out to Tear and fulfils the line.
-
Into the heart he thrusts his sword, into the heart, to hold their hearts
He does this too with Callandor, on purpose to fulfil the prophecy.
-
chains the Shadowsworn to his will from the city, lost and forsaken
I don't remember if he did this remembering the prophecy, but it could be possible.
-
The unstained tower, broken, bends knee to the forgotten sign
Knowing this he forces some to swear loyalty to him.
-
He shall bind the nine moons to serve him
Rand knew this when he started negotiations with the Daughter of the Nine Moons.
There are two parts of the Karaethon Cycle that refers to the Last Battle:
...yet shall the Dragon Reborn confront the Shadow at the Last Battle, and his blood shall give us the Light
and
His blood on the rocks of Shayol Ghul, washing away the Shadow, sacrifice for man’s salvation
This is interpreted as the Dragon Reborn dying in the Last Battle throughout the books. And these lines have Rand troubled on several occasions. But it actually doesn't say that he will die straight out, it only says that he will bleed.
They all knew of the wound Rand had since long, that could never be healed even by Nynaeve.
In addition, Min has vision telling that Alivia will help Rand die. See the wiki about Alivia.
So it seems obvious that the end was something planned in advance by Rand, Min, Alivia and the others, to fulfil the Karaethon Cycle and Min's vision both.