The same way we know about it.
Those fossils that we find in the ground, were also in the ground at the time those guys were carving things into rock, you know.
So just forgetting for a minute the discussion about which animal it depicts. Let's assume for the sake of argument that it is idd a stegosaurus.
Considering the evidence available to us, considering everything we know about geology, archeology, paleontology, physics, biology, zoology, genetics,... etc.
Which is more likely:
- these people walked among LIVING stegosaurus
- these people know about it, just like we do: they found a fossil
By the way... according to everything we know from science, stegosaurus lived some 150 million years ago. While homo sapiens has been around for a good 150.000 years.