Tested in 1.18 thru 1.19.2
I've noticed that this happens once a mob has already started attacking, not before that. I have a villager zombification/curing station wherein a villager in a minecart is in the corner and a zombie is on the outside of the corner. With the flick of a lever, the blocks on one side of the corner are pulled away, allowing the zombie to attack. If I deactivate the lever and push the zombie back outside BEFORE he attacks the villager, he will go idle, but if I let him attack the villager even one time before pushing him outside, he continues to attack the villager through the corner until the villager is zombified. See attached screenshots: when the wall is open, the villager is being attacked. Even after I close the wall, the zombie continues attacking over and over.
This is still happening in 1.19.2. If my villagers get accidentally zombified, they lose all their gossips as I'm running around the world taking care of other business. When I go to cure them again, they are back to a major_positive value for my UUID of 20 instead of 100. I have to use commands to edit their Gossips data and restore it to 100 after curing.