In OR/AS, the last time you encounter Wally is on Victory Road, in a room filled with red flowers. These flowers are actually called spider lilies, and there is a myth that they only grow where you are about to see someone for the last time. This implies that after you win the league, Wally, who has some kind of ailment since the beginning of the game, finally succumbs to his illness which is also the reason he doesn't age in Sun/Moon but red does. When you encounter Wally at the Battle Tree, you are battling his ghost because Wally is dead.