Is Gender Reveal Part of the Unseen?
Question

How can we reconcile between the fact that doctors can find out whether the foetus is male or female, and the verse (interpretation of the meaning): “and [He] knows that which is in the wombs” [Luqman 31:24], and what was narrated in Tafsir Ibn Jareer from Mujahid, that a man asked the Prophet (peace and blessings of Allah be upon him) what his wife would give birth to, and Allah revealed this verse, and what was narrated from Qatadah (may Allah have mercy on him)? Is there any verse or hadith to make the general meaning of this verse (“what is in the wombs”) more specific?
Praise be to Allah.Is there any contradiction between an unambiguous statement of the Quran and reality?
Before I discuss this matter, I would like to explain that there can never be any contradiction between an unambiguous statement of the Quran and reality. If there appears to be a contradiction with reality, it is either because what we think is reality is a baseless claim, or the (text of) the Quran is not opposed to this reality in a clear and definite manner. The unambiguous statements of the Quran and the facts of reality are both definitive, and two definitive things can never contradict one another. 
Is gender reveal allowed in Islam?
Based on the above, it may be said that now they (doctors) are able, through the use of precise machines, to discover what is in the wombs and to find out whether it is male or female . If what is said is false, then there is no point in discussing it. If it is true, then it does not contradict the verse, because the verse is referring to a matter of the unseen, which has to do with the knowledge of Allah about these five matters . 
The matters of the unseen as far as the foetus are concerned are: how long he will remain in his mother’s womb, his life, his deeds, his provision, whether he is doomed (destined for Hell) or blessed (destined for Paradise), and – before it is fully formed – whether it will be male or female. But after it has been fully formed, knowledge of whether it is male or female is no longer the matter of the unseen , because once it has been fully formed it becomes the matter of the visible world, but it remains within the three layers of darkness which, if they were removed, it would be known what it is. It is not far-fetched to think that among the things that Allah has created are powerful rays which can penetrate these layers of darkness so that it may become clear whether the foetus is male or female. The verse does not clearly refer to knowledge of whether the foetus is male or female, and the Sunnah does not refer to that either. 
Is Gender Reveal Part of the Unseen?
With regard to what the questioner refers to, that Ibn Jarir (may Allah have mercy on him) quoted from Mujahid (may Allah have mercy on him) that a man asked the Prophet (peace and blessings of Allah be upon him) what his wife would give birth to, and Allah revealed this verse, this narration is interrupted, because Mujahid (may Allah have mercy on him) was one of the Tabi`in [the generation after the Companions]. 
The Tafsir of Qatadah (may Allah have mercy on him) may be interpreted as meaning that Allah Alone has knowledge of that so long as it has not yet been fully formed. But once the foetus is fully formed, then others may have knowledge of it. 
Ibn Kathir (may Allah have mercy on him) said in his Tafsir of the verse from Surah Luqman (interpretation of the meaning): “No one knows what Allah wants to create in the wombs apart from Him, but once He decrees that it should be male or female, doomed or blessed, the angels who are appointed in charge of it know that, and whoever else He wills among His creation.” 
With regard to your question about what may make the general meaning of this verse (“what is in the wombs”) more specific, we say that if the verse was speaking about being male or female after the foetus is fully formed, then that which would make it more specific would be tangible facts. The scholars have mentioned the principles of making the general meanings of the Quran and Sunnah more specific, which are a text, Ijma` (scholarly consensus), Qiyas (analogy), tangible facts or reason. Their comments on this matter are well known. 
But since the verse does not refer to after the foetus is fully formed, but rather it refers to before that time, there is nothing in it that would contradict the idea of knowing whether the foetus is male or female. 
How to interpret things that happen which outwardly appear to contradict the Quran
Praise be to Allah, there is not, and never will be, anything in reality that contradicts the unambiguous statements of the Quran . The accusations made by the enemies of the Muslims against the Quran, that things happen which outwardly appear to contradict the Quran , arise from their misunderstanding of the Book of Allah which stems from their bad intentions. But the committed Muslims and scholars have enough knowledge to discover the truth which will refute the specious arguments of these people. Praise and blessings be to Allah. 
With regard to this matter, people may be one of two extremes, or a middle group. 

One extreme is to adhere to the apparent meaning of Quranic verses which are not unambiguous, and to reject any certain facts that go against it. Such people either expose themselves to criticism, or they expose the Quran to criticism, because their view contradicts the certain realities of everyday life. 
The other extreme is to turn away from that which is indicated by the Quran and to adopt a purely materialistic approach. Thus one becomes a heretic. 
The middle path is to accept both the indications of the Quran and the facts of reality. These people know that both of them are true, and that the unambiguous statements of the Quran cannot contradict something that is well known and visible. They combine acceptance of the transmitted texts with reason, thus keeping their religious commitment and their intellect safe and sound. Allah guides those who believe to the truth of that wherein they differ, and Allah guides whom He wills to a Straight Path (cf. Al-Baqarah 2:213). 

May Allah help our believing brothers to achieve that, and may He cause us to be guided and to guide others, and make us righteous leaders. My strength is only in Allah, in Him I put my trust and to Him I turn in repentance.