Mirages happen when the ground is very hot and the surrounding air is comparatively cooler. The hot ground warms a layer of air just above it which is optically rarer compared to the cooler air further above.
When the light moves through the cooler air into the layer of hotter air, it is gradually refracted upward. The light then seems to have come from the ground. And it appears as if there is a pool of water on the ground and tricks us. This is how mirages occur in deserts.