The mechanical clock was one of the most important inventions of the later Middle Ages. They slowly changed the understanding of time from something based on the sun and the moon to something traced by the hands of a clock -- until clocks came along, there were always twelve hours of daylight and twelve of night, so the length of the hour varied with the seasons. To make them, a class of craftsmen skilled in making gears and springs and other mechanical parts grew up, and later lent their skill to many other devices. The early surviving clocks are also beautiful, like the one above in Wells Cathedral, England.
The clock in Prague Cathedral.
Bern, Switzerland. These clocks all date to the 1400s or later 1300s. The earliest medieval clocks had wooden mechanisms, which of course did not last very long.
The key invention that made these clocks possible was the "escapement", a mechanism which allowed a swinging pendulum to control the advance of a geared wheel. You all remember from physics class that a pendulum of fixed length has a fixed period, that, is, every swing takes the same amount of time. (Not exactly, in the real world, but pretty close.) The geared wheel thus rotates at a fixed rate, and it could be used to drive the hands of a clock. The pendulum was coupled with a falling weight mechanism that gradually fed energy into the system, keeping the pendulum swinging. This example is actually a seventeenth-century refinement called the "deadbeat escapement," but the medieval "verge escapement" worked on the same basic principle. This system seems to have been perfect around 1280, because over the next 50 years dozens of these clocks were built all over Europe.
All of the parts of these clocks had been known for centuries, including escapements of various kinds, but the famous showpiece clocks built for Caliphs and Chinese emperors all relied ultimately on the flow of water, so they were not truly mechanical devices. This is a drawing from a manuscript of 1206 CE showing the Elephant Clock belonging to one of the Baghdad Caliphs.