While 6 months (or older!) would be better, there are worse things than weaning at 5 months
Is the current barn set up such that mare/foal can be separated for feedings, and more ideally, separated overnight? That would make you taking the filling away that much easier on both of them.
I’d do as suggested - start Ulcer/Gastrogard (at a preventive dose for a foal) a few days before the event, keep it up for probably even a couple weeks after, then just do it. Many, many foals are weaned that way, by being taken from their dams and moved to a new farm.