The Priestly creation narrative really needs to be read without modern cosmological concepts and in a translation that better represents the Hebrew (e.g. many English translations render the Hebrew words 'rts and shmym as "earth" and "heaven" in some verses and "land" and "sky" in other verses), otherwise it can be interpreted in a manner very different from what the author originally intended. For example, the summary statement in v. 1 is often misconstrued as referring to a creation of heaven and earth prior to the creation of sky and land on the second and third days.
The creation of light is what sets forth the rhythm of time, the temporal cycles of day and night. Earth and heaven are then created in the next two days and then these three frames (day/night, heaven, earth) are populated in the final three days. The luminaries are created to populate not only heaven but also the day and night. But light itself preexists even the creation of earth and heaven. The sun is created to rule the day (v. 18), but day itself is already in existence.
From the point of view of ancient cosmology, this might reflect the fact that the sun isn't the only source of light during the day. The blue sky seems to shine with its own light, which brightens when the sun makes its apperance in the morning and it cooperatively darkens when the sun makes its departure in the evening. Even without the sun in the sky, a similar cycle of day and night is presumed to happen in the days preceding the creation of the sun.