Before sunrise and after sunset, the upper atmospheric layers are warmer than the layers near the earth’s surface. So, the atmospheric layers near the earth’s surface are denser than those above.
When the Sun is just below the horizon, the light from the Sun while coming towards the earth suffers refraction from a rare to a denser layer and so it bends towards the normal at each refraction.
Due to the continuous bending of light rays at different successive layers, the Sun can be seen even when its actual position is just below the horizon.
As a result, the Sun is seen in advance a few minutes before it rises above the horizon in the morning. Similarly in the evening, the Sun is delayed by 3 to 4 minutes longer above the horizon after sunset.