From this question Does a soul have any gender? it is known that soul doesn't have any gender. But when a child is born from mother's womb it does have a gender (male, female, or neuter). So, why is a gender-less soul converted to a specific gender? I think it is based on Karma, but does any scripture explain this?
We can get an answer from the embryological point of view as found in an Upanishad called the Garbha Upanishad (which is linked to the Krishna Yajur Veda).
In the fourth month, belly and hip are formed; in the fifth month, the backbone is formed; in the sixth month, nose, eyes and ears are formed.
In the seventh month, [the embryo] comes to have the jīva (conscious self), and in the eighth month, it becomes complete in every sense.
If the father’s seed is more potent, it becomes male; if the mother’s seed is stronger, it becomes female. If the seeds are equal, it becomes an intersexual (napu÷saka, neither male, nor female).
If [at the time of impregnation] the parents are agitated, the child will be blind, crippled, hunch-backed or stunted. If the vital air moves around, the seed enters in two parts, resulting
Additionally this Upanishad also states that a child can remember his past life in the 9th month.
Whatever is consumed or drunk by the mother passes through the nerves and vessels to the child, becoming the source of his satisfaction. During the ninth month, all outer signs attain completeness. And he is reminded of his previous birth, and recounts the good and bad deeds committed.
He thinks: I have seen thousands of wombs, eaten several kinds of food and sucked many breasts. Born and dead again and again, I am immersed in grief but see no remedy. Thinking of my good and bad deeds, I am suffering alone, although the bodies that enjoyed the fruits are gone.
But, it forgets everything, as soon as it comes out of the mother's womb, due to the influence of the Sutika Vayu and that of Mahamaya.
I have similar verses from Tantras as well, but right now can not post them, because the books are not with me right now.
Source- The Garbhopanishad.. You can get the Sanskrit verses from this PDF but i am not able to paste them here (getting some weird characters).
पुमान् पुंसोऽधिके शुक्रे स्त्री भवत्यधिके स्त्रियाः ।
समेऽपुमान् पुं।स्त्रियौ वा क्षीणेऽल्पे च विपर्ययः ॥ ४९ ॥
pumān puṃso'dhike śukre strī bhavatyadhike striyāḥ |
same'pumān puṃ|striyau vā kṣīṇe'lpe ca viparyayaḥ || 49 ||
A male child is born when the man’s seed is in excess, and a female child when the woman's seed is in excess; when the two are equal, there is born either a non-male or a boy and a girl; when it is weak and small in quantity, there is failure.—(49)
The ‘excess’ meant here is not that in quantity, but that in virility.
The verse is from Manusmriti, Chapter 3.
Gita gives more generalised perspective. A "soul" (viz. Purusha) takes birth in certain womb due to the 3 modes.
BG 13.22 - Purusha being situated in Prakruti, experiences the [sattva, rajas, tamas] modes born of Prakruti. Contact with the these modes, is the cause of its births in good and evil wombs.
Good/Evil womb can be further classified to genders, animals, deities, ancestors etc. types of wombs. Chapter-14 discusses, how these 3 modes affect person's next birth during death time.
Usually it's the mind with senses which gets attracted towards certain body:
BG 15.7 -- Among the living world, the living being (bhuta = body) is my eternal fragment only; It (fragment or body) keeps attracting 5 senses & mind as the 6th towards the material nature.