First I'll address "How is it just to punish a child [sic] who did not have any participation in the sin?". I think this answer comes from Rambam's Moreh Nevuchim, but it may be from Gemara (I'll try to find the source).
Punishing a child for their parent's sin seems like something unjust, but in reality it is the parent who is bringing about spiritual-harm to their children through sinning and teaching their children to sin, not a direct punishment of a child for the sins their parent committed.
Let's use smoking as a physical example. If a parent is a smoker, their children are at risk for a number of physical issues from birth-defects to respiratory problems. The child themselves never smoked a cigarette and had no control over their domestic situation, but their parent's negligent-actions directly affects their health. Spiritually, sins work the same way. When a parent sins, they directly affect their children's spiritual-health via "second-hand-sinning". In most cases, a child can undo the harm of their parents' sins by refraining from doing that sin (teshuva), however this is frequently difficult for children to do when they are raised in a sinful environment.
As for the Mamzer aspect of the question, in the Moreh Nevuchim, Book 3, chapter XLIX, where Rambam is discussing Chukim, he says:
In order to create a horror of illicit marriages, a bastard was not
allowed to marry an Israelitish woman (ibid. xxiii. 3): the adulterer
and the adulteress were thus taught that by their act they bring
upon their seed irreparable injury. In every language and in every
nation the issue of licentious conduct has a bad name; the Law
therefore raises the name of the Israelites by keeping them free
from the admixture of bastards. The priests, who have a higher
sanctity, are not allowed to marry a harlot, or a woman that is
divorced from her husband, or that is profane (Lev. xxi 7): the
high-priest, the noblest of the priests, must not marry even a
widow, or a woman that has had sexual intercourse of any kind
(ibid. xxi. 14). Of all these laws the reason is obvious. If bastards
were prohibited to marry any member of the congregation of the
Lord, how much more rigidly had slaves and handmaids to be
excluded. The reason of the prohibition of inter-marriage with
other nations is stated in the Law: "And thou take of their
daughters unto thy sons, and their daughters go a whoring after
their gods, and make thy sons go a whoring after their gods"
(Exod. xxxiv. 16).
Also, Shabos 55a explains that Ezekiel 18:20 teaches there is no death without sin. A father won't die from his son's sins and a son won't die from his father's, everyone dies because they have sinned on their own, even Moshe and Aaron. It doesn't really have to do with punishing a child for their father's sin.