There is an Old Earth Creation (OEC) perspective that provides an explanation for the OP question about these seemingly contradictory verses from Genesis 1. The most well known proponents of this perspective are from the scholars at https://reasons.org and is something that I have been trying to elaborate on in my website at https://big.bible. In this answer I will call it the OEC perspective for brevity, although there are other OEC perspectives among scholars.
This OEC perspective interprets Genesis 1:1 as describing the creation of the universe. This would include the creation of the stars and sun before any other events that are described in the following verses in Genesis 1.
This perspective then takes a special note of Genesis 1:2 (ESV).
"The earth was without form and void, and darkness was over the face of the
deep. And the Spirit of God was hovering over the face of the waters."
From this OEC perspective, this verse sets the point of view or frame of reference for the rest of Genesis 1. The point of view is from the "Spirit of God hovering over the face of the waters", where the "face of the waters" is the early water-covered earth. In other words, the rest of Genesis 1 is told from the point of view of being within the earth's atmosphere, where the Spirit of God is hovering over the waters covering the earth.
From this perspective, the day 1 description of "Let there be light" is the period in earth's transformation where the dense cloud cover changes from nearly opaque to more translucent. This causes the light of the sun to become more visible on the earth through the cloud cover. The day 2 description of "Let there be an expanse" is the formation of the atmosphere that separates the cloud cover above and the water-covered earth beneath the atmosphere. The day 3 description of "Let the waters under the heavens be gathered together into one place, and let the dry land appear" is the appearance of the original continents along with the beginning of primitive plant life.
The day 4 event is initially described in Genesis 1:14-15 (ESV) as follows.
And God said, “Let there be lights in the expanse of the heavens to separate the day from the night. And let them be for signs and for seasons, and for days and years, and let them be lights in the expanse of the heavens to give light upon the earth.” And it was so.
From this OEC perspective verses 14 and 15 describe the sun, moon, and stars being made visible on the earth because of a dissipation of the thick cloud cover and the greater clarity in the atmosphere. From a scientific perspective, this clearing of the atmosphere has been modeled from an increase in oxygen and a decrease in methane. This visibility of the sun and moon from the earth and the increase in oxygen allow plants to begin photosynthesis and sets the stage for the type of animal life that uses the diurnal cycle (i.e., regular periods of light and darkness).
The next verse of Genesis 1:16 is the origin of the quote in the OP which is consistent with the KJV. For consistency in this answer, Genesis 1:16 is shown below from the ESV.
And God made the two great lights — the greater light to rule the day and the lesser light to rule the night — and the stars.
The word that is translated "made" here is the Hebrew word וַיַּעַשׂ which has the root of עשׂה (asah). This has Strong's number of H6213. Honestly, I am not enough of a Hebrew scholar to say much definitively about the nuances of this translation and I don't know how to link to references on the web that use Hebrew in this SE editor. But a search on the web for these Hebrew words does seem to show evidence that וַיַּעַשׂ might be rendered as "had made" instead of "made". If such a rendering were valid, then Genesis 1:16 could be rendered as "And God had made the two great lights...". Such a rendering would be consistent with the beginning of the description of the day 4 event in Genesis 1:14-15 as described above. The phrase of "Let there be..." in verse 14 would then describe the appearance of the lights. Verse 16 would then describe more specifically what those lights were - "the greater light to rule the day and the lesser light to rule the night — and the stars". On the other hand I have not yet seen a commonly used translation that renders Genesis 1:16 with the words "had made" instead of "made". Note that the answer from @Jess in this post would also support the use of "had made", however.
It is also interesting to note that the Hebrew word עשׂה (asah) is different than the Hebrew word בָּרָא (bara) which is used in Genesis 1:1, 1:21, and 1:27 to denote significant creative events. This OEC perspective finds this important since עשׂה (asah) could be used to describe the lights appearing as described above. If the author of Genesis wanted to denote creation of the sun, moon, and stars, then the creative phrase בָּרָא (bara) could have been used instead. Other perspectives like Young Earth Creationism do not find this differentiation between עשׂה (asah) and בָּרָא (bara) to be significant (e.g., https://answersingenesis.org/genesis/did-god-create-bara-or-make-asah-in-genesis-1/).
This all explains how this OEC perspective supports the assertion that the two verses in the OP are not a contradiction. From this OEC perspective, these two verses describe events that are consistent with certain time periods during the creation of the universe and the 4.5 billion year transformation of the earth as described by science. And in this perspective, the order of the events as described in Genesis 1 also corresponds to the order of events as described by modern day science. Hence from this OEC perspective, the OP verses from Genesis are consistent with the scientific findings of the sun being created before the earth.