This is a command from Allaah to His believing slaves to lower their gaze
Ibn Katheer (may Allaah have mercy on him) said:
This is a command from Allaah to His believing slaves to lower their gaze and avoid looking at that which is forbidden to them, so they should not look at anything except that which Allaah has permitted them to look at.
If their gaze happens to fall upon something haraam, then it is unanimously agreed (among the scholars) that if a person’s gaze falls upon something haraam unintentionally, then he should look away immediately.
It was narrated by Muslim in his Saheeh that Jareer ibn ‘Abd-Allaah al-Bajali (may Allaah be pleased with him) said: I asked the Prophet (Sallallahu ‘alaihi wa sallam) about an accidental glance, and he told me to look away.
Tafseer Ibn Katheer, 3/282