Sex chromosomes evolved not once, but many times, independently … stuff happens …
Sex chromosomes are derived from autosomes and have evolved independently many times in different lineages. For example, the human X and Y chromosomes originated about 200-300 million years ago in eutherian mammals after the split of monotremes,
and sex chromosomes evolved independently in birds, snakes, and multiple times in other reptiles, amphibians and fish; they also formed repeatedly in many invertebrate taxa and plants
Full article: https://www.ncbi.nlm.nih.gov/pmc/articles/PMC4120474/