Riolu's Egg originally came from the Sinnoh region. Despite being passed from hand to hand for a long time, it seemed unwilling to hatch. Eventually, it came under the care of the Vermilion City Nurse Joy.
When Ash battled Hashiba, the Egg sensed the strong Aura emitted by him and reacted to it. Ash also felt the Egg calling for him. While Pikachu was being treated at the Pokémon Center, Ash followed the sensation to the room where the Egg was being kept. As the Egg reacted to Ash's presence by rolling towards him, Nurse Joy decided to entrust it to him.
The following night, the Egg hatched into a Riolu, who instantly fled from Sakuragi Institute and started attacking various wild Pokémon, although all of them were too strong for it. Ash finally caught up to Riolu as it was getting attacked by an Onix. During the battle against the Rock Snake Pokémon, Riolu was impressed by Ash's Aura. With both Pikachu and Riolu attacking it, Ash was soon able to drive the Onix away, and then took Riolu to the Pokémon Center to be healed. After Nurse Joy had treated Riolu, it expressed willingness to be caught by Ash, which he agreed to.
Riolu, like every other member of its species, responds to Aura, and, as such, developed a subconscious connection to Ash before it even hatched. After hatching, it displayed a very headstrong personality, attacking wild Pokémon and even Ash, as if it had something to prove. Despite being a baby Pokémon, it displays dislike towards being cared for and generally seems very independent.