Riolu's Egg originally came from the Sinnoh region. Despite being passed from hand to hand, it was taking an abnormally long time 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 Ash, Nurse Joy decided to entrust the Egg 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, has had a subconscious connection to Ash since 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.