This happened to us at the grocery store. Then an old lady said hi to him and he ran back to me. I tried to make riding the grocery cart fun so I had him in front of me with his feet at the bottom of the cart like he was pushing the cart with me. If it's the store or library I usually give him a task like holding something or finding something to keep him close and focus
Hey momma! Yess It happened to us many times our 2 year old is doing same in public places like parks, she just wanted to explore on her own independently so we just let her do what she wants and keep and eye on her. So,Just let them do things in safer environment where they can explore more!