The mirage is caused by the total internal reflection of light at layers of air of different densities.In a desert, the sand is very hot during day time and a result the layer of air in contact with it gets heated up and becomes lighter. The lighter air rises up and the denser air from above comes down.
As a result, the successive upper layers are denser than those below them. A ray of light coming from a distant object, like the top a tree, gets refracted from a denser to a rarer medium.
Consequently the refracted ray bends away from the normal until at a particular layer, the light is incident at an angle greater than the critical angle. At this stage the incident ray suffers total internal reflection and is reflected upwards.
When this reflected beam of light enters the eyes of the observer, it appears as if an inverted image of the tree is seen and the sand looks like a pool of water.