Though Hajj is not compulsory for these two groups it is quite valid if they perform one, but it will not suffice them as regards the Hajj prescribed in Islam.
Ibn ‘Abbas reports: “The Prophet (peace be upon him) said: ‘Any minor (child) who performs Hajj must perform it again after coming of age; any slave who performs Hajj and is then freed, must perform his Hajj again’.” (Reported by At-Tabarani on sound authority) As-Sa’ib bin Yazid says: “My father performed Hajj with the Prophet (peace be upon him) who was performing his Farewell Hajj, while I was only seven years old.” This was reported by Ahmad, Bukhari and Tirmizhi, who noted: “All scholars agree that if a minor performs Hajj, he must repeat his Hajj upon coming of age. Likewise a slave who performs Hajj, and thereafter gains his freedom, must also repeat his Hajj if he is in a position to do so.”
It is also reported from Ibn ‘Abbas that during a Hajj a woman lifted a boy, and asked the Prophet, “Will this boy be rewarded for Hajj?” The Prophet (peace be upon him) answered: “Yes, and you too will be rewarded.” (The boy will be rewarded for his obedience, while the mother will be rewarded for helping and instructing him to perform Hajj. This hadith is also reported from ‘Umar)
Jabir reports: “We performed Hajj with the Prophet (peace be upon him) and a number of women and children were with us. We said talbiyah on behalf of the children, and (also) threw pebbles on their behalf. This is narrated by Ahmad and Ibn Majah.
If a minor is able he may himself declare the state of ihram and perform the rites of Hajj. Otherwise, his guardian (An-Nawawi said: “The guardian who declares the state of ihram on behalf of a non-discerning minor, is the guardian of his properties, i.e., his father, grandfather, or the guardian appointed by law. Concerning the mother, the scholars differ. Some are of the opinion that her ihram on his behalf is only valid if she is appointed by law as his guardian. Others claim that her ihram is valid even if she is not the guardian) must declare the state of ihram and say talbiyah on his behalf, circumambulate round Ka’bah, run between Safa and Marwah, stay at ‘Arafah and throw stones on his behalf. If, on the other hand, a minor attains the age of puberty on or before the Day of ‘Arafah, his Hajj is then credited to him. The same applies to a slave who is liberated on or before the Day of ‘Aratah. Malik and Ibn Al-Munzhir say: “To both of these people their Hajj cannot be credited as (replacement for) the obligatory Hajj, for they intended a supererogatory worship while putting on ihram; its supererogatory status cannot be transformed into an obligatory worship.”