Praise be to Allah.
The scholars differed concerning the attribution of an illegitimate child, if his mother was not married at the time when she committed zina (fornication/adultery) – as is the case here. The majority of scholars are of the view that he is not to be attributed to the zaani.
Some scholars are of the view that if the zaani (one who committed zina) acknowledges the child, it is permissible to attribute him to him. This view was favoured by Shaykh al-Islam Ibn Taymiyah and his student Ibn al-Qayyim (may Allah have mercy on them both).
Ibn Taymiyah (may Allah have mercy on him) said:
There are two scholarly opinions regarding the case where the zaani acknowledges his son, if the woman is not married. The Prophet (blessings and peace of Allah be upon him) said: “The child is for the (owner of the) bed (the husband) and the adulterer gets nothing.” So he attributed the child to the husband and not to the zaani. If the woman is not married, then this hadith does not apply, and ‘Umar attributed children who were born during the Jaahiliyyah to their fathers.
End quote from Majmoo‘ al-Fataawa (32/112-113)
Ibn al-Qayyim (may Allah have mercy on him) said:
Ishaaq ibn Raahawayh was of the view that if the child who is born as a result of zina was not born to a married woman, and thus could not be attributed to the husband, and the zaani acknowledged him, then he was to be attributed to him. … This was also the view of al-Hasan al-Basri; it was narrated from him by Ishaaq ibn Raahawayh with his isnaad that a man committed zina with a woman and she gave birth to a boy, and the zaani acknowledged him. He said: He is to be flogged and the child is to be attributed to him. This is also the view of ‘Urwah ibn az-Zubayr and Sulaymaan ibn Yasaar, from whom it was narrated that they said: If a man comes to a boy and claims that he is his son and that he committed zina with his mother, and no one else claimed this child, then he is his son.
End quote from Zaad al-Ma‘aad (5/381)
One of the most famous proofs given by those scholars who are of this view is the verdict of the Rightly Guided Caliph ‘Umar ibn al-Khattaab (may Allah be pleased with him) in a similar case. It was narrated from Sulaymaan ibn Yasaar that ‘Umar ibn al-Khattaab (may Allah be pleased with him) used to attribute children born during the Jaahiliyyah to those who claimed them (as their sons) in Islam. Narrated by Imam Maalik in al-Muwatta’ (2/740). See also the answers to questions no. 33591 and 176722
This view is more likely to be correct because that deed (fornication) had been committed by the two parties before Islam. It is on this basis that the action of ‘Umar (may Allah be pleased with him) mentioned above is to be understood.
Al-Maawirdi (may Allah have mercy on him) said:
With regard to the response to the hadith narrated from ‘Umar (may Allah be pleased with him), according to which he attributed the children of women who had been prostitutes during the Jaahiliyyah to their fathers after Islam: that had to do with fornication with prostitutes during the Jaahiliyyah, not after Islam; the ruling on fornication during the Jaahiliyyah was more lenient than the ruling on fornication after Islam. Therefore there is some doubt concerning this issue, and where there is doubt, it is permissible to attribute the child to the man who claims he is his son. But it is different when there is no doubt about the issue after Islam.
End quote from al-Haawi al-Kabeer (8/162-163)
The basic principle with regard to the child mentioned is that he should be attributed to his mother and not to you, but if you want him to be attributed to you or there is a shar‘i interest to be served by attributing him to you, such as encouraging the child to become Muslim, or if the law in your country obliges you to attribute him to yourself, if paternity can be proven by means of testing, then we hope that there will be no blame on you for attributing him to yourself and following the view of those scholars who allowed that, as mentioned above.
If he is not attributed to you, for some reason or other, or you do not want that, then there is no blame on you in this case either. Rather this is the basic principle and it is the view of the majority of scholars.
The basic principle with regard to custody of a child before he reaches the age of discernment is that he should stay with his mother, then when he reaches the age of discernment he may be given the choice between his parents. But if there is any risk of harm to the child if his mother is given custody of him, or if there is a greater purpose to be served by the child staying with his father, then custody is to be given to the father if possible.
Please see the answer to question no 153390
If you hope that your being given custody of the child or his being close to you will benefit him in religious terms, or encourage him to become Muslim, then you should strive to have him near you and soften his heart towards your religion, in the hope that Allah may save this soul from the Fire through you.
With regard to your situation, we think that the best way to follow is for you to try hard to take care of him and soften his heart by showing kindness and compassion, and showing that you take an interest in his affairs, so that he will see you as a good example of Islam and so that he will miss you if you are absent and will be happy to see you if you are present. This will open the child’s heart to listening to your advice and following it.
Allah, may He be exalted, says (interpretation of the meaning):
“And by the Mercy of Allah, you dealt with them gently. And had you been severe and harsh-hearted, they would have broken away from about you; so pass over (their faults), and ask (Allah’s) Forgiveness for them; and consult them in the affairs. Then when you have taken a decision, put your trust in Allah, certainly, Allah loves those who put their trust (in Him)”
[Aal ‘Imraan 3:159].
But if the law does not allow you to have custody of him and you are not able to have any influence over him, then we hope that there will not be any blame on you for that, in sha Allah. Islam erases whatever came before it, and this child is the outcome of something that happened at a time of Jaahilyyah the sin of which Allah has erased from you, in sha Allah, by your becoming Muslim.
So now you should focus on doing good in the future and making up for what you missed out on with regard to this child by raising your legitimate child properly and being kind to him.
With regard to meeting the child’s mother, you can do that in the presence of a third party, such as your wife or mother, for example, or a mahram (close relative) of hers, so that you will not be alone with her in the manner that it is prohibited.
And Allah knows best.