Mirages occur when ground heat creates a temperature gradient in the air, with hot, less dense air near the surface and cooler air above. Light rays refract (bend) as they pass through these layers of different densities, creating distorted or displaced images of distant objects.