Ezekiel 18:20
20 The soul that sinneth, it shall die. The son shall not bear the iniquity of the father, neither shall the father bear the iniquity of the son: the righteousness of the righteous shall be upon him, and the wickedness of the wicked shall be upon him.
Exodus 20:5
5 Thou shalt not bow down thyself to them, nor serve them: for I the Lord thy God am a jealous God, visiting the iniquity of the fathers upon the children unto the third and fourth generation of them that hate me;
Exodus 20:5 - which is reiterated in Exodus 34:7, Numbers 14:18, and Deuteronomy 5:9 - makes it seem like God is doing what He said He would not do in Ezekiel 18:20 - and similarly in Jeremiah 31:29-30, Deuteronomy 24:16, and Ezekiel 18:2-4. So, how are these passages understood in light of one another?
Notice that the "son shall not bear the iniquity of the father", and vice versa. One cannot inherit another person's sin, because they did not do it.
If a person's mother is an alcoholic, those sins are not in any way imputed to the child. The child is 100% innocent, and therefore does not personally bear the iniquity of their parents.
However, does that baby suffer as a result of his mother's wickedness? Yes. Is her iniquity being visited on him? Yes.
For one, that baby might have Fetal Alcohol Syndrome, or some other defect, from his mother's drinking. So too if he has bad parents, he will suffer the effects of the bad parenting. The sin of bad parenting, then, is being visited on him. There are practically innumerable such examples of a person's sins being visited on another innocent party.
God has built in these punishments as a way of making sure a certain group of people cannot thrive with certain evil practices. Unfortunately, the children are often collateral damage. The innocent always suffer at the hands of the wicked, and the Lord understands this and has differing expectations for different people (Luke 12:48).
The other aspect of how iniquity is visited upon generations of people is due to practices that become common in families. If one's parents promoted a culture where drinking is normal/encouraged, the children will probably follow in their footsteps, and that cycle may perpetuate itself for generations.
The same goes for idolatry. Most people follow the religion of their parents and ancestors. In this sense, the mistakes, sins, and iniquity of their ancestors are visited on them because those wicked people are depriving them of truth and keeping their progeny in sin and darkness from beyond the grave.
Then, as mentioned before, as the children join in and progressive generations continue the idolatry, God will not cease to be a righteous judge. Those children eventually become accountable for their own sins, even though they can point their finger directly at, for instance, their grandfather, who converted to a false religion, setting the new norm for the family.
No, these passages do not contradict. The innocent might suffer fallout from their wicked ancestors, but until they join in themselves, willingly, they are not held individually accountable for anything their mother, or father, or grandparents did.