A Master (Japanese: マスタートレーナー Master Trainer) is a Trainer class that a Trainer acquires when they become a Champion of battling and/or an 'elite' specialist in a Pokémon type. This is something that can be acquired either when they gain the title of a Gym Leader, Elite Four, Frontier Brain or Champion. Most of these members, among others are Master's of certain types, although being a Master is not compulsory in order to receive a title such as Gym Leader.
A Mega Master (Japanese: メガマスタートレーナー Mega Master Trainer) is a Trainer class that a Trainer may acquire once they've gained the ability to Mega Evolve their Pokémon and own a Key Stone. The title can only be obtained once all 3 things have been accomplished. Being able to Mega Evolve Pokémon and/or obtain a Key Stone does not require any exam, however is a rare occurrence due to the abundance of known Key Stones.