Great question. We have to look at the whole forest, not just one leaf. Mark has been making a point over the course of a number of chapters that mere good works doesn't cut it.
* Mark 7.1-23: The heart matters, not just obeying the commandments and living by the traditions. "It is what comes out of a man that mess him unclean" (v. 16).
* Mk. 7.24-30: The crux is the heart, not the rituals. Even a Gentile woman could show faith and receive blessing.
* Mk. 7.31-37: The point is that people with ears have not understood. The people of the Law have rejected him, but whoever will HEAR and DO is the true person of God.
* Mk. 8.1-13: Jesus gives a great sign from heaven, and the Pharisees then ask for a sign. How blind are they? Obedience to the Law has not brought them into relationship with God or led them to eternal life.
* Mk. 8.14-21: Jesus rebukes even his own disciples for hardness of heart: eyes that fail to see, ears that fail to hear. The signs are clear that eternal life is found in relationship with Jesus alone, not by keeping the Law. "Beware the yeast of the Pharisees."
* Mk. 8.22-26: Jesus heals a blind man as a living parable that He can open the eyes of the blind. It's a physical event with a spiritual message.
* Mk. 8.31-9.1: Jesus is the Christ (vv. 31-9.1). It is those who follow him who find eternal life.
* Mk. 9.2-13: Coming right after the recognition of who Jesus really is, it is now shown what he is here to do: reveal God, suffer and die, and rise from the dead. This is where eternal life is found. This message is authenticated by God the Father. The story is dripping with all kinds of symbolism
* Mk. 9.14-32: What follows now is a series of discipleship training episodes—what it really means to be a follower of Jesus who will inherit eternal life. Here the message is FAITH.
* Mk. 9.33-37: Here the message is HUMILITY
* Mk. 9.38-41: Here the message is LIVE AND ACT IN THE NAME OF JESUS
* Mk. 9.42-50:Here the message is GET RID OF OBSTACLES to a relationship with Jesus. And DON'T BE AN OBSTACLE TO SOMEONE ELSE.
* Mk. 10.1-12: They try to trap him with the minutia of the Law. Jesus will not be trapped. It's not about the Law but rather about the intent of God (vv. 5ff.) behind the Law.
* Mk. 10.13-16: Children pose the perfect illustration of what Jesus has been saying. Those who inherit eternal life are humble, receptive, trusting, teachable, dependent, loving, and obedient.
And so we come to the tale of the rich young man. "What must I do to inherit eternal life?" In other words, "What works are necessary"? Sigh. Jesus has been answering this question over and over. So first Jesus questions whether he understands goodness, which the man does not. His reply is subtle but clear: heaven is not earnable; your goodness doesn't and can't cut it. It's not about goodness.
Jesus rolls through some of the commandments, particularly the ones that relate to how people treat people. (Notice he hasn't mentioned the ones about a person's relationship to God, the first 4 commandments).
The man seems pleased. He's been a good guy. Jesus doesn't knock that, but it has no value as far as securing eternal life. There is nothing in keeping the commandments (being good) that works in your favor for salvation. You can score all the touchdowns you want while you're playing sandlot football, but it doesn't count for anything unless you're on the team in the stadium. Jesus has been hammering away at this point for chapters now. Now Jesus has to move this man to understanding that.
The man thinks that his wealth (blessing), influence and position (blessing), Jewishness (advantage), and lawfulness (earning his way) are going to do it for him. It's a great temptation to trust in possessions, status, and privilege.
The man's shortcomings are great. He has not really obeyed the commandments, because he had excess while others were poor. He lacked self-denial and self-sacrificing devotion. He was expected to die to everything in his life and follow Jesus (Mk. 8.34-37).
In other words, in the Matthew text (the one you quoted, Matthew 19.17), Jesus is leading him into truth one step at a time. When the man answers (Mt. 19.18-20), Jesus uses that as a launch point to take him further and deeper. Matthew's flow of logic and structure are similar enough to Mark's (I used Mark because of the OP) that you get the idea. We can't just pull out one portion of one verse and think we understand what's happening. There's a whole flow here. And the man had obviously had some exposure to the teachings of Jesus (he has heard other things, not just this) because of the way he approaches and addresses Jesus. We are not to think this story exists in a vacuum.