The Pokémon franchise is well known for its tricky timelines, with multiple canons across the games, so you'd be forgiven for thinking that the anime, with what little continuity it has, would be more straightforward. Unfortunately, it turns out the anime's just as convoluted thanks to the seeming inconsistencies of time travel. With Pokémon like Dialga and Celebi able to freely travel the fourth dimension at will, Pokémon would be missing out on huge story opportunities if they didn't cross eras at least once. But how exactly does time travel work in the anime?
Firstly, it's important to realize that there are three distinct methods of time travel in the Pokémon universe, each with a different result. Strong emotions result in spontaneous travel and new timelines, Dialga can send whoever whenever it wishes, while Celebi's often a fellow traveler.
The strong emotions method of time travel has been used in two notable situations, firstly, in Season 9, Episode 12, "Time-Warp Heals All Wounds," where May, Squirtle and Meowth find a locket that transports them back in time, then again in Season 18, Episode 32, "Rotom's Wish!" Here, the connecting factor is strong emotions connected to a specific date and time -- Edna, the locket's owner, visited a train station daily as she waited for her deceased husband to return, constantly thinking of the day he left; while Rotom's regret at running away from the fateful battle that lost his partner their hotel allows it transverse the decades.
Either way, these feelings that allow for spontaneous time-travel often appear when things have gone horribly wrong, and require some sort of fixing -- whether it's through Squirtle's Ice Beam turning the rain to snow so that the train that would be delayed, or Ash helping Rotom's trainer with a strategy to win the battle, thereby defending the hotel from criminals.
While things turn out for the betterment of everyone involved, the fact that it creates a branching timeline raises some uncomfortable questions. For example, when May returns to the present, everything about the town is completely different -- except for her friends, whose only difference is that they don't remember how the town originally was. This begs the question: what happened to this timeline's version of May?
Perhaps she was also sent back in time -- but thinking it would snow anyway, she didn't have Squirtle use Ice Beam and thus, when she arrives in the bad timeline, she believes it is her fault that things ended up so terribly. At least when Ash and Co. timeline-hopped in XY they did it as a group, and thus avoided some of this confusion.
Slightly more deliberate and understandable from a lore standpoint, is Dialga, the timely representative of the Creation Trio of Legendries. Dialga has surprisingly few instances where it takes full advantage of its mastery of chronology, with the most notable example being in Pokémon: Arceus and the Jewel of Life, where it sent Ash and co. back with the express purpose of changing the past.
Unlike the emotional version of time travel though, Dialga's power allows multiple timelines to continue forward at once, with Ash's adventure in the past happening concurrently with the Creation Trio's battle with Arceus in the present, and upon the group's return, Dialga melds the timelines, rather than simply replacing them. What this means is that everyone remembers both timelines -- not just the time-traveling characters, and the restoration isn't instant but gradual.
But while changing the past may make things messy, when it comes to protecting the current timeline, Celebi is by far the most reliable. With jumping through time being Celebi's key defense from attack, it makes sense that the mythical Pokémon would want to know where it was jumping to, and thus preserve the integrity of events.
Celebi's is the only time travel method that results in a stable time loop, such as what occurred in Pokémon 4Ever - Celebi: The Voice of the Forest where Celebi brings a young Samuel Oak into the present to assist Ash in saving the forest -- something that the current Oak reminisces about at the end of the film.
Likewise, in Season 22, Episode 33, "A Timeless Encounter!" despite the accidental nature of Celebi flinging Ash back in time, the loop is still completed when it's revealed that Ash was a huge inspiration for Professor Kukui's love of Pokémon battles and moves -- something he demonstrated throughout all of Sun and Moon. While a Celebi does help Ritchie change the past in Chronicles, Episode 11, "Celebi and Joy!" it's worth noting that rather than taking Ritchie directly there, it leads him to a carving of itself made by an elderly Nurse Joy's deceased childhood friend -- something that has very strong emotions attached to it.
Whether it be through strong regrets wishing to change the past to better the future, or being sent back to save the world twice in a row by Dialga, or even just taking a lovely stroll through eras past and future with Celebi, the Pokémon anime has no shortage of timeless stories to tell, each with its own internal logic.