My observation is that this happens when the AI does not see (line of sight) any enemies (player characters).
So if you hide in the shadows at the end of your turns, the enemies are generally confused to death. Not always: if they are close, they will sometimes move to look for you.