As with so many things, the concept of sex is a little more complicated than most people imagine. To understand that, you have to know how sex is determined….
It starts with whether a zygote, and therefore later an embryo and a fetus, has an SRY gene which leads to developing as male. Normally, the SRY gene is carried on the Y chromosome, so if you have a Y chromosome then you also have an SRY gene so you develop as a male but, because SRY can be missing from the Y chromosome and may be present on the X chromosome, someone can be XY genetically and develop as female and can be XX and develop as male. Then there are people with three of these chromosomes: XXY and XYY and other strange combinations. So if we defined “biological sex” as chromosomal sex, already we have a few people who don’t fit with it, in that their apparent sex doesn’t match their chromosomes.
Next, the SRY gene really only controls how the embryonic gonads develop by steering their development into testes which then produce testosterone. In the absence of SRY, they develop into ovaries. There is also a mechanism here where the male fetus produces anti-mullerian hormone. Between that and testosterone, this steers the development of the rest of the reproductive system down the male path. If these hormones are absent, or at a very low level, the reproductive system develops as female. So immediately this opens up two more ways in which things can go awry: The testes may not produce as much testosterone as they should, or other tissues in the body may not be sufficiently sensitive to it, so a fetus that is genetically male is then born apparently female.
Finally, testosterone acts on the brain while it is still development in the fetus and causes some parts of it to develop as male or female. We also know that gender identity is already present at birth because babies look at different things depending in their sex, i.e. before society has placed any expectations on them, they are interested in different things. This seem by far the mostly cause of gender dysphoria, i.e. where someone feels like one sex, because that is how the brain developed, but has physical characteristics of the other sex.
Because different things develop at different times: for example, IIRC the difference in brain development happens later, if the testosterone level varies during the course of fetal development, the brain can end up out of step with the body.
So we even have to be careful about what we mean by “biological sex”.