I never knew this for a long time.

The closing price of a share or index is taken as the average price traded in the last 30 minutes. So the closing price of a share or a index is not taken as the price traded when the market closes (i.e 3:30 PM).

The closing price is taken as the average traded price between 3PM and 3:30 PM.

Lets assume Nifty rises evenly from 5000 to 5050 between 3 PM and 3:30 PM. The after hours closing price of Nifty for the day is taken as 5025, which is the average mean of 5000 and 5050.

The after hours closing price adjustment happens at around 4 PM (30 minutes after the market close)