The marathon is named after the legend of Philippides, an ancient greek messenger who apparently ran to Athens without stopping to claim victory in the battle of Marathon.
However, nobody really knows how far his route was, and so the modern marathon was actually not a standard distance when it began as an Olympic sport in 1896.
The 1908 Olympics in London had a marathon course designed to finish in front of the royals which was 42.195km, and that eventually became the standard marathon distance which is run today.