First of all, we can say that doctors are able to know the gender of the fetus through x-rays and ultrasound scans, but we should realize that this knowledge is still vague and incomplete. They may make mistakes, as has happened on numerous occasions.
Even if they know if the foetus is male or female, they do not know whether it will be miscarried or carried to full-term. They do not know the precise length of time that it will stay in its mother’s womb. They do not know how long this person will live, how he will behave or what his provision will be, or whether he will be of the people of Paradise or the people of Hell.
In this context, the prominent Muslim scholar, Mufti Ebrahim Desai, states: Allah says among the five things that only Allah knows, one is, (And He knows that which is in the wombs.) (Luqman 31: 34) The word ‘that’ in the verse is general and the word ‘wombs’ is plural. Firstly, the scanning machines are not accurate. We have many experiences where the scanning machines were proven incorrect. However, if they are upgraded and are accurate, then too it is not contrary to the verse in the Qur’an.

Is there any machine in the world who can simultaneously say how women in the world are pregnant at the same time? And what will be the gender of the child they will be giving birth to? Assuming that they may produce such a machine that may meet this challenge (albeit farfetched), the world will never be able to produce a machine that can tell us the condition of all the women from the inception of mankind up to the last woman giving birth in this world. The knowledge of all that is only known by Allah and nobody else.
To take it one step further, the verse doesn’t speak only about the gender of the child. It may also include the lifespan of the child, being healthy or unhealthy, rich or poor, successful or unsuccessful, all that is best known by Allah. Modern technology will never ever be able to meet these demands.

Elaborating on this issue, Sheikh Muhammad S. Al-Munajjid, a prominent Saudi Muslim lecturer and author, states the following: Ibn `Umar (may Allah be pleased with him) says that the Messenger of Allah (peace and blessings be upon him) said: “The keys of the Unseen are five, which no one knows except Allah: no one knows what will happen tomorrow except Allah; no one knows what is in the wombs except Allah; no one knows when it will rain except Allah; no one knows in which land he will die; and no one knows when the Hour will begin except Allah.” (Reported by Al-Bukhari)

Al-Bukhari also reported this hadith with the wording: “The keys of the Unseen are five: ‘Lo! Allah! With Him is knowledge of the Hour. He sendeth down the rain, and knoweth that which is in the wombs. No soul knoweth what it will earn tomorrow, and no soul knoweth in what land it will die. Lo! Allah is Knower, Aware.’ (Luqman 31: 34)
Allah is the only One Who knows the Unseen. He Almighty says, (Say (O Muhammad): None in the heavens and the earth knoweth the Unseen save Allah; and they know not when they will be raised (again).) (An-Naml 27: 65)
The Unseen referred to in the aforementioned verse is the same as that described in surat Luqman mentioned above.
Coming more closely to the issue in point, we can say that doctors are able to know the gender of the foetus through x-rays and ultrasound scans, but we should realize that this knowledge is still vague and incomplete. They may make mistakes, as has happened on numerous occasions.
Moreover, they can only perform these investigations after the elapse of a certain number of weeks of pregnancy, and not before. Even if they know if the foetus is male or female, they do not know whether it will be miscarried or carried to full-term, or born alive or stillborn. They do not know the precise length of time that it will stay in its mother’s womb. They have nothing but conjecture and uncertainties. They do not know how long this person will live, how he will behave or what his provision will be, or whether he will be of the people of Paradise or the people of Hell.
Knowledge of what is in the wombs is not merely a matter of knowing whether the fetus is male or female; it is broader than that, and no one can know it all except Allah. He Almighty says, (Allah knoweth that which every female beareth and that which the wombs absorb and that which they grow. And everything with Him is measured.) (Ar-Ra`d: 8)
In his Tafsir, Imam Ibn Kathir (may Allah have mercy on him) comments on this verse, saying: ‘Allah is telling us here of His complete knowledge: nothing is hidden from Him and He knows fully what the females of every species carry in their wombs. He Almighty knows whether the fetus is male or female, good or bad, destined for Paradise or doomed to Hell, have a short life or a long one. Allah Almighty says: (He is best aware of you (from the time) when He created you from the earth, and when ye were hidden in the bellies of your mothers. Therefore ascribe not purity unto yourselves. He is best aware of him who wardeth off (evil).) (An-Najm 53: 32)
Ibn Mas`ud (may Allah be pleased with him) says that the Prophet (peace and blessings be upon him) said: “The way that each of you is created is that he is gathered in his mother’s womb for forty days and then for a similar length of time as a blood-clot and then for a similar length of time as a lump of flesh. Then an angel is sent, and he breathes the spirit into him and is charged with four commands: to write down his provision, his life span, his actions, and whether he will be wretched or happy.” According to another narration, the angel says: “O Lord, male or female? Wretched or happy? How much provision? How long is his life span? And he writes that.” (Reported by Al-Bukhari and Muslim)
Every Muslim should have firm belief, beyond any shadow of doubt, that what the Prophet (peace blessings be upon him) says is ‘wahy’ (Divine Revelation) revealed to him by Allah. Allah Almighty says: (By the Star when it setteth. Your comrade erreth not, nor is deceived;‏ Nor
doth he speak of (his own) desire. It is naught save an inspiration that is inspired.
) (An-Najm 53: 1-4)