Mirage happens due to a total internal reflection of light. The figure below illustrates the mechanism of a mirage being formed in a desert. On a hot sunny day, the air near the surface is hotter and rarer than the air above it. So when light from a distant object goes towards earth, it is actually going from a denser to rarer medium. So it gets reflected back if the angle of incidence is too steep and we happen to observe mirage.