The Monster Manual says that a Flameskull's master "must craft their instructions with care to ensure that the creature carries out its tasks properly" and that the Flameskull "interprets those commands to the letter". From this, we can infer that Flameskulls are literal-minded in the extreme and if they are not told to do something, they will not do it unless it is necessary for the completion of their task. Converely, if they are not told to stop doing something, then it will continue doing it until they can do it no longer. If a Flameskull is commanded "bring me a diamond", it will bring exactly one diamond. If it is commanded "bring me diamonds", then I hope you like being buried under precious stones, because you are going to get all of the diamonds. All of them.
I have not run LMoP, but I would reason as follows:
- Flameskulls do exactly what they are told. They do no more, and they do no less. If the instructions you give are not the instructions you mean, then you should give better instructions next time.
- The skull in the cavern is acting on "ancient instructions to prevent intruders from passing through". Note the lack of instructions to chase intruders, kill intruders, punish intruders who do pass through, steal intruders' underwear, or dance the macarena.
- Valid targets for the skull are therefore "intruders who are trying to pass through". If someone is trying to pass through the room but is not an intruder, they are not a valid target. If someone is an intruder but not trying to pass through the room, they are not a valid target.
- Once someone has passed through the room they are no longer passing through the room, nor are they trying to do so. Therefore, a character that has passed through the room and is not currently crossing back is not a valid target for the Flameskull and will be ignored by the Flameskull. It is not even considered for targeting area effects or multi-target attacks.
- If a character that is not trying to pass through the room is attacking the Flameskull or aiding another character that is trying to pass through, it becomes a valid target again. The character is actively hindering the Flameskull and must be eliminated lest the Flameskull become unable to complete its task. Once the character stops attacking/helping, they are no longer hindering the Flameskull and will once again be ignored.
With this reasoning in hand, I would have the Flameskull break off pursuit and attack of each character individually as soon as the Flameskull can see that the character has passed through the room and shows no intention to cross back. That character is no longer trying to pass through the room and is not what the Flameskull is interested in. If that means that the Flameskull turns away before the zombies, so be it. The behaviour of the zombies and the orders given to the Flameskull are not the same thing. If that means your party runs through the room taking fire until they cross the exit threshold and are then safe, so be it.
Of course, if the party doesn't destroy the Flameskull (complete with holy water/Dispel Magic/Remove Curse), then they have to run the gauntlet on the way out back. Good luck to them.