The impurities in the water causes corrosion and provides circuit paths for the electricity which were not in the phone's operating design and could damage the components. The corrosion starts immediately and is ongoing until it is completely cleaned away
First do not try to charge or to turn on your phone and then remove the battery as soon as possible from the phone to minimize any further damage.
Then you need to dis-assemble the rest of the phone and clean all the affected parts using Isopropyl Alcohol 99%+ (available at electronics parts stores) to remove all traces of corrosion and water. If possible do not use "rubbing alcohol" as in some cases this is only 70% IPA or less, can contain additives and is not as effective. If you do have to use it check the label to verify the amount of IPA. The higher the percentage of IPA the better
Here is a link that describes the process. Electronics Water Damage
As always with electronics repair, especially surface mounted PCBs be gentle when handling and especially when brushing away the corrosion. You do not want to remove any components from the board. Remove the shields that may be covering some components as the water may have got in under there. The ends of any flex cables and their connectors need to be cleaned as well
Hopefully after you have done all this the phone might possibly work correctly again.
Here is a link to the Samsung Galaxy A51 Repair guides that may help.
If this process seems too daunting, take your phone to a reputable, professional mobile phone repair service, experienced in liquid damage repair and ask for a quote for a repair. If you decide to do this, do it sooner than later.