You did the right thing by taking the object away from her and explaining why. It's a classic, lighthearted interaction between a parent and a curious child. Here's how the rest of the conversation might have gone:
You: "Because it's been on the ground, and it might not be clean. We don't want you to get sick."
Your 5-year-old daughter, still curious, might ask, "Why is it dirty?"
You: "Well, the ground is where people and animals walk, and sometimes they might drop things or step on them. So, it's better to play with clean things."
Your daughter nods, understanding, and you continue your walk, keeping a watchful eye on her explorations.
This kind of interaction is not only about safety but also a great opportunity for children to learn about hygiene and making good choices.