I am sorry if this question has been asked already, but I am super confused! Today I saw a gym that had been taken over by team mystic, which I am as well. It was a level 3 gym with only 2 Pokemon in it. I placed my highest Pokemon in there and actually became the gym leader. About a half hour later I was looking at my phone and noticed it was being attacked. I figured it was going to be taken over by one of the other teams. It turned grey and stayed that way for about 5 minutes which was odd. Then It went right back to blue and stayed at a level 3 gym with 3 Pokemon in there. Although I got kicked out! I looked to see what level everyone else's Pokemon were and there was only one that was higher than me. I thought when it was a "friendly" battle that it would just take the prestige points up and open up another slot for another blue team member. Why did I get kicked out?
You got kicked out because the gym was defeated and reduced to level 0. All the pokemon in there were knocked out, one by one. Some of your teammates then "sniped" the gym before whoever defeated it could claim the gym for their own team, and it was fully repopulated with new pokemon.
The attack you noticed was not friendly training, and the gym going grey was the game showing all the old pokemon leaving the gym. If you check usernames, you'll probably see that none of the trainers are the same ones you joined when you defended the gym, and all the pokemon placed there changed too.