The Quran lays down that no one carries another's burden, and that Allah does not punish until a message has reached a person. A child is not yet responsible for belief or deeds. A hadith in Sahih al-Bukhari gives comfort to parents: a Muslim whose three children die before they reach the age of accountability is brought into Paradise by Allah's mercy towards those children. The Quran also says that the offspring of believers who follow them in faith are joined with them in Paradise.
About the children of non-Muslims, scholars have held more than one view, and it is better to say so clearly. Asked about them, the Prophet ﷺ said that Allah knows best what they would have done. In a dream that is recorded in Sahih al-Bukhari, he saw Abraham, peace be upon him, with many children around him, and was told that they were every child who died on the natural disposition (fitrah) to know Allah. When some Muslims asked 'and the children of the polytheists?', he answered: 'And the children of the polytheists.' On this basis many scholars hold that they are in Paradise. Others say they will be tested on the Day of Judgment, and others leave the matter with Allah, as the Prophet ﷺ did.
What all Muslims agree on is the character of the Judge: He wrongs no one even by the weight of an ant, and He is the Most Merciful. A parent who has lost a child can hold on to this, and the Prophet ﷺ himself wept at his son's death and said that the eye weeps and the heart grieves. Grief is not a lack of faith, and the child is in the hands of the Most Merciful.