from bulbapedia
When a Pokémon with Moxie causes another Pokémon (including allies) to faint through direct damage, its Attack rises by one stage. Moxie will not be activated by indirect damage, such as through Sandstorm or an ability like Rough Skin. Moxie will not activate if an allied Pokémon defeats a foe in a double or triple battle.
you will only gain the moxie attack boost by fainting them from a direct attack