Before the railways, every town kept its own clock set by the sun. That worked until trains made it possible to travel far enough, fast enough, for the difference to matter. Timetables were impossible when each station disagreed about noon.
Standard zones were the fix: large bands of territory agreeing to share one clock, each offset from a common reference. The system was formalised internationally in the late nineteenth century and has been refined ever since.
Why the map looks messy
Zones follow politics, not geometry. China spans five geographic hours but uses one official time. India and Nepal chose half-hour and 45-minute offsets. Some island nations shifted across the date line for trade reasons. This is why calculating time differences by hand goes wrong so often.