I know that this is impossible in the games, but I’m wondering if it is able to happen in the anime, for example. I’m just curious.
Without the use of a cheating device to get a Pokemon that's already Mega Evolved or to teach a Pokemon a Z-Move, it is impossible to have a Mega Evolved Pokemon use a Z-Move, because either of the actions would require the item slot. Because of the way menu priority works, a Mega Rayquaza can never use a Z-Move either, because the Z-Crystal out prioritizes the Mega Evolution, and Rayquaza won't Mega Evolve until after the move has been used. Moves like Trick and Bestow can't help here either, because those moves will fail if a Mega Stone or Z-Crystal is attempted to be moved. The closest to this would probably be with Ultra Burst. Other transformations like Battle Bond and Power Construct that don't require items can have their alternate forms use Z-Moves.
in the anime is hard to say. its every inconsistant, but they have got better. I think it is possible for the anime, but not in game unless they change the z-move/mega choice priority (or as I suggest, z-move on bottom half, mega on top like a split button) I hope they do change it in future games. hope I help bro.