Hello Radu,
Your phone has another screen layer known as the digitizer. It essentially keeps track of where the tip of your pen is when you press it on the screen. The software in your phone communicates with the pen and displays what you write or tap on your screen. While it sounds like your top screen is damaged and doesn't respond correctly to touch, your digitizer is still working independently with what's inside your pen.
I hope this helps answer your question.