Step 1: On a very hot day, the layer of air lying just above a road or sandy surface gets heated strongly by contact with the hot ground, while the air higher up stays cooler.
Step 2: Hot air is less dense than cool air, and less dense air has a lower refractive index. So as we move from the upper cooler layers down toward the ground, the refractive index of the air keeps decreasing.
Step 3: Light coming from the sky travels down through this stack of air layers of gradually changing refractive index. At each boundary it bends slightly away from the normal, since it is passing into a layer of lower refractive index. After enough of these small bendings, the ray curves so much that it undergoes total internal reflection in the hot air layer near the ground and turns back upward.
Step 4: This upward turning ray then reaches the observer's eye. Since the brain always assumes light has travelled in a straight line, it interprets the ray as if it had come from below the ground, from a point on the surface. The eye perceives this as a shimmering, mirror like patch, which looks exactly like a pool of water reflecting the sky.
So a mirage is caused by the unequal heating of different layers of air near the ground, which sets up a refractive index gradient and leads to total internal reflection of sky light.