Pokemon Go has hidden a pretty awesome surprise in their new gym system. Feeding a Pokemon in a friendly gym will occasionally cause the Pokemon to give players candies, even if they don't own that specific Pokemon. Players will also earn a small amount of Stardust if they receive a Candy from a Pokemon.
Feeding Pokemon is part of a new "motivation" mechanic that's a major part of the new gym system. Each defending Pokemon has a motivation stat that controls when they get kicked out of a gym. Pokemon lose motivation slowly over time or in large chunks whenever they lose a gym battle.
However, players can restore a Pokemon's motivation by feeding them berries. Players can feed any Pokemon that belongs to your in-game team (Team Valor, Team Mystic, or Team Instinct) to help them stay in gyms longer.
The candy perk seems to be designed to motivate players to feed any Pokemon they meet. Since most gyms are filled with Pokemon like Dragonite and Tyranitar that are hard to get candies for, this also serves as a way for lower level players to get the candies they need to evolve their own Pokemon and jump into the metagame more quickly.
So far, there doesn't seem to be a "trick" to getting candies from gyms. Players don't need to have a specific Pokemon assigned as their partner to get the candy, nor do they have to feed a Pokemon a specific kind of berry. It seems to be totally random, so it's also possible that you can feed a Pokemon multiple berries but still not get any candies in return.
This is a great new mechanic and is an easy way for players to earn otherwise hard to get candies. We'll have to see if there's any other secrets lurking inside Pokemon Go's new changes.
More Pokemon: Players Worldwide Will Benefit from Pokemon Go Fest Rewards / Pokemon Go Update Spurs Speculation on New Shiny Pokemon / Pokemon Go Warns Cheaters That Their Pokemon Will Start "Misbehaving" / Pokemon Go Makes Huge Changes to Attack Bonuses / More Evidence of Legendary Pokemon Found in Pokemon Go's New Update / Pokemon Go Is Transforming Tons of PokeStops into Gyms / First Look at Pokemon Go's New Items / Pokemon Game Confirmed in Development for Nintendo Switch