The soul who sins shall die. The son shall not suffer for the iniquity of the father, nor the father suffer for the iniquity of the son. The righteousness of the righteous shall be upon himself, and the wickedness of the wicked shall be upon himself.
You shall not bow down to them or serve them, for I the Lord your God am a jealous God, visiting the iniquity of the fathers on the children to the third and the fourth generation of those who hate me, but showing steadfast love to thousands of those who love me and keep my commandments.
The claim: Exodus says that the sin of the previous generation will get passed down to the children, who will also get punished for it. Ezekiel says that the son of the father who sins will not be punished for their father's sins.
The Explanation: The entire chapter of Ezekiel 18 is about a proverb that will no longer be getting passed down. This proverb is "the fathers have eaten sour grapes, and the children's teeth are set on edge," which is found in verse 2. This proverb is saying that the sins of the father are passed down to his children. The context of Ezekiel 18:2 is God asking why this proverb has been getting passed down.
The whole purpose of Ezekiel 18 is to say that the soul that sins in the one who will die (verse 20). This is actually consistent with what Exodus 20:5 is saying. Exodus 20:5 says, "visiting the iniquity of the fathers on the children to the third and the fourth generation of those who hate me." This statement is saying that the children of the third and fourth generation also hate God. In other words, the children are sharing in the same sin. For instance, the father is worshiping a false idol (or whatever sin). The third and fourth generation is also worshiping this false idol (or whatever sin). In this case, since the third and fourth generation is practicing this sin, they will be punished because they were practicing it, not because their fathers were.
Exodus 20:5 goes on to say, "showing steadfast love to thousands of those who love me and keep my commandments," and this is consistent with Ezekiel 18:20. There is no contradiction.