There are very few Abilities which actually have a deactivation effect, but it would be interesting to know whether the Illusion bug affects these Abilities too:
- If you hack Flash Fire as the base Ability, get hit by a Fire-type move, and then Mega Evolve, do you still have the Flash Fire boost?
- If you hack Primal weather as the base Ability, does the weather stay when you Mega Evolve? If another user of the same weather switches in and out while the Mega stays in does the weather stay?
Flash Fire and extreme weather Abilities require the Ability to be active for the effects to be active. In fact, extreme weather ends before the Mega Evolution animation even begins. What makes Illusion different is it has to be working
before the Pokemon switches in (and as you know, Abilities "don't exist" while Pokemon are inactive since Gen 5). My theory is that Illusion has a separate check, that the Pokemon started the battle with Illusion and that it hasn't forme changed in such a way that its Ability was changed in the process (so, Mega Evolution and Shaymin Sky Forme reverting to Land Forme).
It's the only way to explain this nonsense I tested in Gen 6:
- Illusion Meloetta disguised as Lucario -> use Relic Song -> Lucario transformed! (still looks like Lucario)
- check summary, says it has Serene Grace now -> attacking it breaks the Illusion, but Skill Swap fails on it
- switch out and back in, and suddenly it's disguised as Lucario again -> Skill Swap succeeds this time and the Illusion wears off, but the swapped Ability is actually Serene Grace
This is even easier to see in Gen 7 since clicking the Pokemon's minisprite on the bottom screen to check its stat stages/Ability/effects never lies to you about its current Ability, unlike the summary screen.
So, basically, the reason a Mega-Evolved Pokemon behind an Illusion before it switches doesn't come back in disguised is because the game
knows its base Ability was supposed to change and this deactivates Illusion's second check (for whatever reason; I don't have a disassembly). Like I alluded to above, whenever any forme change other than Mega Evolution or freezing Shaymin-Sky happens, your Ability isn't updated. But, if you switch out at any point after changing formes (even if you change back to the forme you started as beforehand), the game will change your Ability to the correct Ability slot for that species, without updating the second Illusion check because the game just isn't designed that way. Realistically, this only makes a difference for Meloetta (because it doesn't need its Ability to forme change) and Darmanitan (because it has more than one possible Ability, so Zen Mode can become Sheer Force if Zen Mode was hacked in a non-Hidden slot).
Edit: Upon further inspection, and because of the very last thing I said here, it turns out that what the game is actually doing with Meloetta and Darmanitan's Abilities when they forme change without the correct Ability + slot combination is simply renaming the Ability. So in the test I did above, despite Illusion being named Serene Grace, it's actually still functioning as Illusion, besides being able to be overwritten/swapped as Serene Grace would. I went back to the slot 1 Zen Mode Darmanitan test and after the first forme change, I switched it out and back in, and now both the summary screen and stat stages screen claim it has Sheer Force. After using Flame Charge and having its Speed rise, and ending the turn and seeing "[Darmanitan's Sheer Force] Zen Mode triggered!" happen, I think it's pretty clear Zen Mode is what it really has.
Another edit: Upon more inspection, Skill Swapping these "fake" Abilities will make them start working normally again (Serene Grace and Sheer Force). Transforming into them while the Ability is in this glitched state also gives the Transformed Pokemon the right Ability and its functionality. Switching Meloetta/Darmanitan out and back in just restarts the glitched Ability behaviour.