In such a situation an application for discontinuance of the proceedings should be made. You can legally leave the territory of Poland if you have a valid residence title, e.g.: residence card, visa, under the visa-free regime, having a stamp in your passport confirming the application for legalization of your stay.
In this situation, you can also apply for a refund of the stamp duty – you can find the form here (‘Podanie o zwrot opłaty wniesionej na konto Mazowieckiego Urzędu Wojewódzkiego’).