Jaeger
That someone better
...Even more generally, Attacks of Opportunity or Opportunity Attacks were added to 3rd edition and retained in 5th edition because movement was and is free. ...
I think it is also an artifact of AC being a passive defense. There are really no rolls to Defend in D&D.
An enemy goblin runs at you, rolls to hit, then as they only used half their movement, they keep going past!
AoO is a way to not allow Monsters or PC's to get in a "free hit".
...So yes, the basic core reason was to make "entering melee" a significant decision. It wouldn't feel right if your foe could just leg it with you being unable to do anything about it.
...
It's interesting that in real life you opponent can do exactly that!
From a gaming perspective I understand the reasoning behind AoO. But IMHO, sometimes game designers overthink stuff a bit too much.
But having played many games that do not have AoO, I find it largely an artifact of D&D style systems.