Thanks for the reply. Just to be more informative, I will describe all the troubleshooting I have already done after talking to apple support:
Screwed the pencil tip tightly.
Made sure that pencil firmware is up-to-date.
Installed a fresh copy of iPadOS using iTunes and recovery mode.
Tried resetting network settings.
Charged the iPad to more than 80% and left the pencil attached to magnetic strip for 45 mins. The purpose of this step, according to Apple, is to update the pencil firmware.
Had Apple support run diagnostics on the iPad.
Nothing has worked so far. The pencil remains connected as long as it is attached to the magnetic strip. Once it is removed, it immediately disconnects.