The teaching on this passage from such as Martin Luther (see link below) is that the rich young ruler approaches Jesus as 'good Master'.
The young man sees only a master who can instruct him with legal commandments. All he thinks he needs is the knowledge of good and evil. He thinks he has resource within himself to do all that is necessary - he just needs guidance as to what to do.
He hasn't seen the necessity of a Saviour. He doesn't know of his desperate need within. He has not (yet) appreciated that God has sent One to save - not to legally instruct.
For this is why the Son of God came forth.
He is not a second Moses.
For the law was given by Moses, but grace and truth came by Jesus Christ. John 1:17 KJV.
So Jesus responds by speaking to where the young man is. And gives him food for thought according to his present spiritual state.
Why call me good ?
None is good, but God.
The man who thinks he needs only legal commandments is a man who knows only of a distant Deity, unknown, unrevealed. He does all himself (so he thinks). He has yet to discover what Saul of Tarsus discovered :
O wretched man that I am ! Who shall deliver me ? Romans 7:24 KJV.
Martin Luther "A Treatise on Good Works
We ought first to know that there are no good works except those which God has commanded, even as there is no sin except that which God has forbidden. Therefore whoever wishes to know and to do good works needs nothing else than to know God's commandments. Thus Christ says, Matthew xix, "If thou wilt enter into life, keep the commandments." And when the young man asks Him, Matthew xix, what he shall do that he may inherit eternal life, Christ sets before him naught else but the Ten Commandments. …