The Earth's surface is constituted of several tectonic plates, large segments of the planet's outer layer, which move against each other. This movement is responsible for various geological phenomena, such as earthquakes, volcanoes, and the formation of mountains and ocean basins.
The tectonic activity in Morocco primarily involves the convergence of the Eurasian and the Nubian (African) plates. The Eurasian Plate pushing against the Nubian Plate is what led to the formation of the Atlas Mountains, which run through Morocco, Algeria and Tunisia. The mountains are where the epicenter of this recent earthquake was.
Currently, the collisions between the plates are causing a shortening of the Atlas Mountains, explaining the area's seismicity. We know this because of data from GPS measurements, which show that they are moving about 1 millimeter closer to each other every year.
This shortening and compression is causing what are known as faults, huge friction between plates. These faults are the likely cause of this earthquake. Scientists think that these faults have been active for a long time, going back a few million years.
In addition, as pointed out by various researchers, the High Atlas Mountains have a unique geological feature where the Earth's outermost and hard layer, called the lithosphere, is thinner than usual, combined with an unusual rise of the mantle. All these features could have influenced the occurrence of this high magnitude earthquake.