I'm talking about abilities such as Moxie or Grim Neigh. They raise stats when Pokemon are knocked out, but under certain circumstances, they don't raise a stat. What are all the circumstances of this?
When a Pokémon with Moxie [or a similar ability] directly causes another Pokémon (including allies) to faint by using a damaging move, its Attack [or whatever stat the ability raises] is increased by one stage. If its Attack [or whatever stat the ability raises] is already at +6 stages, Moxie [or a similar ability] will not activate.
If a Pokémon with Moxie [or a similar ability] defeats a Pokémon with Mummy using a contact move, the Pokémon's Ability will become Mummy before Moxie [or a similar ability] has the opportunity to activate.
Generation VI onward If the Pokémon fainting ends the battle, Moxie [or a similar ability] will not activate.
source