Praise be to Allaah.
Imaam Abu Abdullaah Muhammad ibn Ismaaeel al-Bukhaari reported, in al-Jaami al-Saheeh, which is the soundest book after the Quraan:
"Ibraaheem ibn al-Mundhir told me that Man told us that Maalik told me, from Abdullaah ibn Deenaar from Ibn Umar (may Allaah be pleased with him), that the Messenger of Allaah (Peace & Blessings of Allaah be upon Him) said: The keys of the Unknown are five, which no-one knows except Allaah: no-one knows what will happen tomorrow except Allaah; no-one knows what is in the wombs except Allaah; no-one knows when it will rain except Allaah; no-one knows in which land he will die; and no-one knows when the Hour will begin except Allaah." (Saheeh al-Bukhaari, Kitaab Tafseer al-Quraan, hadeeth no. 4328).
He also reported it with the wording: "The keys of the Unseen are five: Verily, Allah! With Him (Alone) is the knowledge of the Hour, He send down the rain, and knows that which is in the wombs. No person knows what he will earn tomorrow, and no person knows in what land he will die. Verily Allaah is All-Knower, All-Aware (of things) [Luqmaan 31:34]." (Saheeh al-Bukhaari, Kitaab Tafseer al-Quraan, Baab (wa indahu mafaatih al-ghaybi laa yalamu illaa huwa), hadeeth no. 4261).
Allaah is the only One Who has knowledge of the Unseen, as He says (Interpretation of the meaning) :
"Say: None in the heavens and the earth knows the Unseen except Allaah . . ." [al-Naml 27:65] The Unseen referred to in this aayah is the same as that described in Soorat Luqmaan (see quote in preceding paragraph).
There is no doubt whatsoever that this hadeeth is saheeh. What has confused the questioner is the fact that doctors are able to know the gender of the foetus via x-rays and ultrasound scans, but we should note that this knowledge is vague and incomplete. They may make mistakes, as has happened on numerous occasions. Moreover, they can only perform these scans etc. after a certain number of weeks of pregnancy have passed, and not before. Even if they know whether the foetus is male or female, they do not know whether it will be miscarried or carried to term, whether it will be born alive or dead. They do not know the precise length of time that it will stay in its mothers 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 foetus is male or female; it is broader than that, and no-one can know it all except Allaah, as He says (Interpretation of the meaning):
"Allaah knows what every female bears, and by how much the wombs fall short or exceed. Everything with Him is in (due) proportion." [al-Rad 13:8]
In his Tafseer of this aayah, Ibn Katheer, may Allaah have mercy on him, said: "Allaah 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 says (Interpretation of the meaning): (He) knows that which is in the wombs, i.e., He knows whether it is male or female, good or bad, destined for Paradise or doomed to Hell, whether its life will be long or short. Similarly He says (Interpretation of the meaning): He knows you well when He created you from the earth, and when you were foetuses in your mothers wombs [al-Najm 53:32]
In the two Saheehs there is a report from Ibn Masood, who said: The Messenger of Allah (Peace & Blessings of Allaah be upon Him) said: "The way that each of you is created is that he is gathered in your mothers 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 hadeeth, the angel says: O Lord, male or female? Wretched or happy? How much provision? How long a life span? and he writes that."
Al-Awfi said, from Ibn Abbaas: "and by how much the wombs fall short" refers to miscarriage, and or exceed means how how far beyond the normal time the pregnancy will last before a child is born, because some women carry a child for ten months and some for nine months; the length of pregnancy varies from woman to woman. This is what is referred to here. Al-Dahhaak said, from Ibn Abbaas, that this phrase referred to pregnancies longer or shorter than nine months."
Every Muslim must believe in what the Prophet (Peace & Blessings of Allaah be upon Him) has told us, and not doubt a single word of it, because what the Prophet says is wahy (revelation) revealed to him by Allaah:
"By the star when it goes down, your companion (i.e., Muhammad) has neither gone astray nor has erred. Nor does he speak of (his own) desire. It is only an Inspiration [wahy] that is inspired." [al-Najm 53:1-4]
Whoever doubts what the Prophet (Peace & Blessings of Allaah be upon Him) said or doubts that it is wahy is a kaafir who has left Islam.
The Muslim must believe that what the Prophet (Peace & Blessings of Allaah be upon Him) said is absolutely true. This is shown in the hadeeth of Abdullaah ibn Amar, who said: "I used to write down everything that I heard from the Messenger of Allaah (Peace & Blessings of Allaah be upon Him), because I wanted to memorize it. Quraysh told me not to do that and said: Are you writing everything down that you hear? The Messenger of Allaah is only a man who may speak when he is angry or happy. So I stopped writing things down, and I mentioned this to the Messenger of Allaah (Peace & Blessings of Allaah be upon Him). He pointed to his mouth and said: Write; by the One in Whose hand is my soul, nothing comes out of it (my mouth) but the truth." (Reported by Abu Dawud, may Allaah have mercy on him, in al-Sunan, Kitaab al-Ilm, hadeeth no. 3161, see also Saheeh al-Jaami, no. 1196).
We ask Allah to protect us from the whispers of Shaytaan and to give us strength of faith. May Allaah bless our Prophet Muhammad.