As with any piece of literature, to best understand its statements you must read them in context. The Bible is no different. Matthew 5:16 is in the context of the beatitudes (Matt. 5:1 - 7:29) where Jesus is teaching proper, good, and moral behavior. Disciples of Jesus are to be lights; that is, doers of good. 1 Peter 2:12 is where Peter is admonishing the Christians to live godly and holy lives before the unbelievers. We Christians live in the world among unbelievers and they are going to see how we live. Peter is telling us to act properly with unbelievers so that false accusations will not stand against us because we have lived with integrity among them.
By contrast, Jesus in Matthew 6:1-4, which is still in the same beatitudes where He told people to let their light shine before people (5:16) is stating that if the motive of doing something good is to be noticed by people, then don't do that. Don't boast about how "good" you are before people. That is wrong. There is nothing wrong with doing good works that will be seen before people, after all, we live among unbelievers. But, when you do good things, don't do them for the purpose of drawing attention to how "good" you are.
Likewise in Matthew 23:3,5 Jesus is addressing the crowds and teaching them about the hypocrisy of the Scribes and Pharisees and how they do their deeds in order to be noticed and admired. Jesus condemned this as is right.
People are supposed to notice your good works because your good character permeates them, not because of your attempt to have them see how "good" and "great" you are. The former is humility. The latter is prideful and wrong. The context of each verse tells us this.
Note: scripture quotes are from the NASB. Scripture links are to the KJV.