The Marathon race originates from the legend of Pheidippides, an Athenian herald (hemerodromos). In 490 BC, after the Greek victory over the Persians at the Battle of Marathon, he ran ~25 miles (40 km) from Marathon to Athens to announce "Nenikēkamen!" ("We have won!"). He collapsed and died of exhaustion.
The modern marathon (42.195 km / 26.2 mi) was standardized at the 1908 London Olympics (Windsor Castle to White City Stadium). The distance commemorates the legend, not the exact historical route.
Note: Herodotus describes a longer run (Athens to Sparta, ~240 km) by Pheidippides before the battle to seek aid; the Marathon-to-Athens run appears in later sources (Plutarch, Lucian).