The Prophet (peace be upon him) remained at Arafat until after the sun had completely set, and then he departed for Muzdalifah. This established the Sunnah of remaining at Arafat until sunset. The scholars differ on the ruling for a pilgrim who departs before sunset and whether a penalty is required.
The Hanbali school, followed by Ibn Baz and Ibn Uthaymeen, holds that remaining at Arafat until sunset is wajib (obligatory), and leaving before sunset without returning requires a dam (sacrifice of a sheep). Their evidence is that the Prophet consistently remained until sunset and said: 'Take your rites from me.' If the pilgrim returns to Arafat before sunset — even briefly — no penalty is due. Sheikh Ibn Baz emphasized that the Prophet's practice of staying until sunset is binding, not merely recommended.
The Hanafi school considers remaining until sunset wajib as well, and requires a dam for leaving early. The Maliki school holds a similar position. The Shafi'i school, however, considers remaining until sunset as Sunnah mu'akkadah (emphasized Sunnah) rather than wajib, meaning it is highly recommended but no dam is required for leaving early — only the loss of the Sunnah's reward. All scholars agree that the wuquf (standing) itself is valid even if the pilgrim left before sunset, as long as they were present at Arafat at some point after zawal. The Hajj is not invalidated by early departure.