In John 20:17 Mary was not allowed to touch the Lord, but in John 20:27 Thomas was invited to touch Him. Why?
The solution to the apparent conflict is found in the Greek language. Our English Bibles do not help us understand what Jesus actually said.
Mary Touches Jesus
The first passage you refer to takes us to the occasion when Mary found Jesus after His resurrection and then started clinging to Him. Here is what happened.
Jesus said to her, “Mary!” She turned and said to Him in Hebrew, “Rabboni!” (which means, Teacher). Jesus said to her, “Stop clinging to Me, for I have not yet ascended to the Father; but go to My brethren, and say to them, ‘I ascend to My Father and your Father, and My God and your God.'” (NASB) John 20:16-17
The key to understanding Jesus is found in the phrase “I ascend.” The tense of the Greek word for “ascend” is a tense which indicates a future event is going to happen as though it were occurring immediately. The tense is called the historic present. What Jesus is doing is encouraging Mary that He is not going to leave immediately. Even though His return to heaven is certain, He is not ascending immediately. She does not need to cling to Him. He will ascend later.
Thomas Touches Jesus
But when Jesus meets Thomas in John 20:26, there is no sense of urgency from Thomas. Thomas is not trying to keep Jesus from leaving. In fact, Thomas has a different problem. He has a problem that some of us have. He is doubting that this person he sees is actually Jesus.
And after eight days again His disciples were inside, and Thomas with them. Jesus came, the doors having been shut, and stood in their midst, and said, “Peace be with you.” Then He said to Thomas, “Reach here your finger, and see My hands; and reach here your hand, and put it into My side; and be not unbelieving, but believing.” Thomas answered and said to Him, “My Lord and my God!” (NASB) John 20:26-28
So Jesus needs to encourage this man and help him understand that He really did return from the dead. Thomas needed proof. It is amazing that Jesus did not rebuke him for not believing. Instead, Jesus responded with love and compassion when He said, “Touch Me!” Thomas did and believed.
There are some Christians who on occasion wonder if Jesus is real. To anyone who may be reading this response and having doubts, we need to add that Jesus is willing to offer proof. He is not with us now, but if you ask Him to reveal Himself in some way, He will. He can be found in the pages of the Bible. He can also be found through answered prayer. Ask Him to reveal Himself, not in a physical form, but through answered prayer or through a special event. I believe He will if you are sincere. Jesus showed compassion to one of His own disciples who should have known Him. How much more will He help those who live some 2,000 years later?