The marathon was invented for the 1896 Olympics, inspired by a 19th-century Victorian poem that confused two distinct legends associated with the battle of Marathon.

What do you think when you listen to the word marathon? 2,500 years after the battle of Marathon (490 BC), most people would think of a long-distance running competition. More than that, we also describe something that requires a lot of effort as a marathon, such as watching all Lord of the Rings movies in one sitting.
As so many things in our modern life, this concept dates back to the 19th century, and more specifically a French philologist named Michel Bréal.

We are in Athens. It's 1896 and we are attending the first modern Olympics.
Of course there were some attempts at 'reviving' this old classical tradition both in Greece (see the Zappa Olympics) and abroad (e.g. the Cotswold Olimpick Games in 17th century England and the Olympiad of the Republic in Revolutionary France).
Back to the 1896 Olympics.
Imagine that you are standing in the Panathenaic stadium among 100,000 enthusiastic attendees who are awaiting to find out who will win the Marathon, a long distance race honoring Phidippides, the ancient Athenian runner-messenger who informed the Athenians of the Persian defeat at the battle of Marathon.
The race began almost three hours ago at the village of Marathon and is supposed to end sometime soon in the stadium where you are, some 42km of distance.

Finally, the Greek runner Spyridon Louis enters the stadium and the audience is ecstatic.
What a fitting triumph for a Greek to win the most Greek of the games. Right?

No ancient Greek had ever competed in a 'Marathon run', simply because such a thing did not exist back then, and was certainly not a part of the ancient Olympic Games.
In fact, not even Pheidippides himself, the person in whose honor the modern marathon was established, had run this distance in the way that 19th century intellectuals thought he had.
The Marathon race is a recent; a very recent invention. The idea belongs 100% to the French philologist Michel Bréal (1832-1915).
Bréal seems to have thought of this game when in 1894 Pierre de Coubertin set up the Olympic Games and founded the International Olympic Committee (IOC).
In a letter to Coubertin, Bréal wrote:
...It goes without saying that I give you my full and complete support for the Committee... "The Games of Peace" form a very beautiful subject, on which there are true and useful things to be said...
Since you are going to Athens, see if a race from Marathon to the Pnyx can be organized. That would have an antique flavor. If we knew the time the Greek warrior took, we could establish the record. For my part, I would claim the honor of offering "the Marathon Cup."
The marathon that took place in the Olympics of Athens in 1896 started in Marathon but ended in the Panathenaic stadium (a monument with its own interesting history), not Pnyx, as Bréal wanted.
However, among the prizes offered to Louis for finishing first was the "Marathon cup" of Bréal. The antique cup was intended to lend some of its classical authority to the game itself.
So, in the end, the race was a success. Louis became the much needed hero of a modern nation looking for much needed national confidence and in the distant classical past. The marathon became a standard of the Olympics.

This is where things get interesting.
Bréal's marathon race was inspired by the story of Pheidippides. The story was the following:
Herodotus tells a different story. He tells us the story of Pheidippides running to Sparta and even meeting the Great God Pan himself on the way. However, the Greek historian doesn't tell us a thing about Pheidippides running from Marathon to Athens and then dying.
Lucian of Samosata (125-180), one of my favorite ancient authors, wrote some five centuries after Marathon, that a man named Philippides (not Pheidippides) ran from Marathon to Athens, proclaimed "Joy, we win" and then died on the spot.
It is not difficult to imagine that the story of Herodotus' Pheidippides and Lucian's Philippides would merge given enough time.
In 1879, the English poet and playwriting Robert Browning (1815-1889) published a poem named Pheidippides, a poem that was going to become extremely popular art a time when the West was falling in love with the battle of Marathon and its powerful symbolism.
The poem fused the ancient tales of Pheidippides and Philippides into one, highly sentimental narrative that birthed the modern legend of Pheidippides, the same tale that a few years later would drive Coubertin and Breal to add the Marathon in the program of the Olympics.
I will quote part of the poem below but you can also find the whole poem online.
Unforeseeing one! Yes, he fought on the Marathon day:
So, when Persia was dust, all cried "To Akropolis !
Run, Pheidippides, one race more! the meed is thy due!
'Athens is saved, thank Pan,' go shout!" He flung down his shield,
Ran like fire once more: and the space 'twixt the Fennel-field
And Athens was stubble again, a field which a fire runs through,
Till in he broke: "Rejoice, we conquer!" Like wine thro' clay,
Joy in his blood bursting his heart, he died--the bliss!
So, in the end the Marathon is a modern invention which appropriates ancient stories and symbols found in classical art and literature to satisfy a modern appetite for "antique flavour".