Amplifying Davels' writeup:

The traditional origin of the marathon running race is considered to date back to the battle between Athens and Persia on the plains of Marathon, about 25 miles outside the city limits of Athens, Greece, in about 500 B.C.

The Athenians sought to repel an invasion by the stronger Persian forces under the command of King Darius. The battle at Marathon was considered to be their last stand, and while the troops fought, the remaining Athenian population prepared to flee the city.

But the battle swung in the Athenians' favor, and the Persians withdrew. The Athenians appointed a young messenger named Philippides (spelled "Phidippides" in some sources) to carry the news back to the city. Philippides ran the 25 miles back to Athens, into the city square, shouting "Nike! Nike!" (Greek for "Victory!") before dropping dead.

But this is only a piece of the story, as the run from Marathon into Athens represented a fraction of Philippides' exertion. Two days before the battle, when the Athenians learned of the coming Persian onslaught, Philippides was dispatched to run to Sparta, some 75 miles away, to request their military assistance. When the Spartans informed Philippides that they were in the middle of a religious festival and couldn't rally an army for several days, Philippides then ran back to Athens with the bad news. He then marched with the Athenians to Marathon, fought in the battle, and made his final run back to the city to announce the victory. All told, he covered more than 200 miles over the course of three days, as well as fighting a battle against a stronger foe, all of which makes his death in the city square seem a little more understandable.

However, there is some reason to doubt the existence of Philippides' famous run. Herodotus, the Greek historian who wrote the most contemporaneous account of the Battle of Marathon about 50 years after the fact, mentions Philippides' run to Sparta, but omits any mention of his final victory run back to Athens. Instead, Herodotus reports that all of the Athenian army ran back from Marathon to Athens, in order to prevent the Persians from sailing around and attacking the city from the sea. The first account of Philippides' final run appears some five centuries later.

At any rate, whether its inspiration came from a jog by Philippides or the entire Athenian army, the modern marathon was first run in the 1896 Olympics in Athens, the first "modern" Olympic Games. At that time, the race was only 24.85 miles (40 km), from Marathon Bridge to the Olympic Stadium in Athens, roughly Philippides' original course. Twelve years later, at the Olympics in London, the race was lengthened by an extra mile and 385 yards, so as to start at Windsor Castle and end in front of the Royal Box at the Olympic Stadium. That distance of 26.2 miles (42.195 km) is now the fixed official distance for all marathons.