To understand this early denial and later permission, one would need to understand about God and Moses and the pattern shown and that this is applicable to Christ Jesus.
According to all that I [God] shew thee [Moses], after the pattern of the tabernacle, and the pattern of all the instruments thereof, even so shall ye make it.
So we find Hebrews saying about the example and shadow and pattern,
Who serve unto the example and shadow of heavenly things, as Moses was admonished of God when he was about to make the tabernacle: for, See, saith he, that thou make all things according to the pattern shewed to thee in the mount.
With these things in mind, the idea of Christ saying to Mary "don't touch" at John 20:17 prior to ascending and "ok to touch" at John 20:27 afterward looks back to this symbolism as an OT pattern for NT atonement.
And there shall be no man in the tabernacle of the congregation when he goeth in to make an atonement in the holy place, until he come out, and have made an atonement for himself, and for his household, and for all the congregation of Israel.
The pattern was "do not touch" before he goes into the Holy of Holies to make an atonement for himself, household, and all the congregation.
And now in the reality of Christ's ministry, this was done about 2,000 years ago once for all time never needing to be repeated or represented.
Who needeth not daily, as those high priests, to offer up sacrifice, first for his own sins, and then for the people's: for this he did once, when he offered up himself.
So, Christ ascended, made purification as the High Priest, and did it for Himself and His people. The key here is our High Priest did it. We can't do it. At the same time, it is clear this is not universal salvation applicable to all, but rather to those who believe it; we are His people.
PS. Matthew 28:9 is not a parallel account of John 20:17. John 20:17 happened shortly after sunset from Sabbath to Sunday. Matthew 28:9 happened around Sunday morning sunrise. There are plenty of reasons like no contradictions that support this, but this is afield of the OP question.