Photo Credit: Lucasfilm
Maz lived on/was visiting Bespin
It’s been said that Maz Kanata is as old as Yoda, so it’s entirely possible that throughout her centuries of life she lived on (or at least visited) Cloud City on Bespin. Maybe she was even present during the same time that Luke and Vader had their first epic duel and sensed the powerful Force artifact that had been left behind with Luke’s severed hand.
As a Force Mystic, it’s possible that she never had any need for the lightsaber, but it’s clear that her Force abilities of foresight and empathy are her focus – maybe she kept the weapon, anticipating Finn’s and Rey’s future need of it.
Luke gave it to her
If Darth Vader took it upon himself to retrieve his old lightsaber after it had fallen into the Cloud City chasm, it’s just as possible that at some point (likely after Return of the Jedi) Luke returned to Bespin to retrieve his first Jedi weapon.
Even if he never intended to use it, the weapon’s history alone would make it a powerful teaching tool in the Jedi Academy that he was in process of establishing. The lightsaber still contains resonance from Anakin’s years as a Jedi, but also the dark energy from Anakin’s first days as Darth Vader – he did use that saber to kill the Jedi younglings, after all.
I could see Luke using that saber in much the same way that Yoda utilized the Dark Side Cave on Dagobah as a means to teach aspiring Jedi to be self aware.
Then in the days after Kylo Ren destroyed the Jedi Academy, Luke gave the lightsaber to Maz as part of his abandonment of the life he was living. To him the weapon would’ve been too keen a reminder of his failure.