From Pope St. John Paul II’s encyclical Veritatis Splendor (1993):
Jesus says: “Why do you ask me about what is good? There is only one who is good. If you wish to enter into life, keep the commandments” (Mt 19:17). In the versions of the Evangelists Mark and Luke the question is phrased in this way: “Why do you call me good? No one is good but God alone” (Mk 10:18; cf. Lk 18:19).
Before answering the question, Jesus wishes the young man to have a clear idea of why he asked his question. The “Good Teacher” points out to him — and to all of us — that the answer to the question, “What good must I do to have eternal life?” can only be found by turning one’s mind and heart to the “One” who is good: “No one is good but God alone” (Mk 10:18; cf. Lk 18:19). Only God can answer the question about what is good, because he is the Good itself.
To ask about the good, in fact, ultimately means to turn towards God, the fullness of goodness. Jesus shows that the young man’s question is really a religious question, and that the goodness that attracts and at the same time obliges man has its source in God, and indeed is God himself. God alone is worthy of being loved “with all one’s heart, and with all one’s soul, and with all one’s mind” (Mt 22:37). He is the source of man’s happiness. Jesus brings the question about morally good action back to its religious foundations, to the acknowledgment of God, who alone is goodness, fullness of life, the final end of human activity, and perfect happiness.
— Veritatis Splendor 9