OEMs these days store lockscreen password on some separate partition => resetting /data and /system doesnt make the phone 'forget' the prev password.
But there is a solution that works on most of the devices (the solution is flashing all partitions without formatting the partitions that store imei). Get the firmware flash tool for your device (eg sp flash for mediatek devices, something similar exists for qualcomm devices too).. A googling in the format "how to flash stock firmware on <your device>" would lead you to the right page.
I can't give any specific guide since you haven't mentioned your device model and there exists no generic guide except the outline I mentioned above.