Step 1: Note where the actual Sun is at these moments.
At the moment we say we see the sunrise or sunset, the Sun is actually still just below the geometric horizon.
Step 2: Recall how the atmosphere is layered.
Air close to the Earth is denser than the air higher up, so the atmosphere behaves like a stack of layers with a gradually changing refractive index.
Step 3: Follow what happens to the Sun's rays.
As the Sun's rays travel obliquely through these layers, they keep bending towards the normal because they are moving from rarer to denser air, and this continuous bending curves the ray path slightly downward towards us.
Step 4: Connect the bending to what we see.
Because our eye and brain always assume light travels in straight lines, the Sun appears to be higher than its true geometric position, so it seems visible about two minutes before actual sunrise and two minutes after actual sunset.
\[ \boxed{\text{atmospheric refraction}} \]