Earthquakes occur on faults.
Because faults have friction, they resist the forces trying to move the
pieces apart. As the forces build, the fault remains locked and the blocks
get deformed because of the increasing stress.
Eventually the stresses get so high that the fault breaks. This releases the built up stress and allows the sides of the fault to slide past one another. This is what we call an earthquake.
The blocks return to their locked state until the stresses build up enough to cause another earthquake.