You might have heard of something called geometry.
Geometry is from the Greek for "earth measurement", and ancient people use it for measuring land, or surveying.
The ancient Egyptians needed geometry to re survey property boundries after each annual Nile flood. So the ancient Egyptians had been using geometry for at least 3,000 years before the Ptolomaic dynasty gained the throne. And the Ptolomies ran Egypt like a money making business. So it seems pretty obvious that Egypt would have been surveyed rather well during the Ptolomaic dynasty.
Of course the Nile river twisted and turned like most rivers do. If property lines were surveyed at right angles to the river's direction at a spot, instead of all going straight east to west, those property lines would not be all equally spaced from north to south and counting them would not give an accurate north-south distance.
Eqypt would have been mapped for military purposes also. The Ptolomies might want to invade the countries to the south as earlier Egyptian dynasties had, and the Nubian kingdoms might want to invade Egypt - there had been a Nubian conquest that put a Nubian dynasty on the Egyptian throne. And the native Egyptians sometimes revolted.
And obviously there was a military benefit to knowing all the sortcuts through the desert that bypassed the meandering loops of the Nile. So any caravan routes through the desert which were shortcuts between loops would have been surveyed to find all the waterholes and estimate how large an army could use those waterholes.
So it is quite likey that Eqypt was surveyed down to Syene, modern Aswan, a frontier town with a Ptolemaic garrison.
And no doubt the government files had distances and routes between various places measured in case of need.
Aswan/Cyene is at 24 degrees, 05 minutes, 20 seconds north, and Alexandria is at 31 degrees, 12 minutes north, or a little more than 7 degrees of latitude farther north.
https://en.wikipedia.org/wiki/Aswan[1]
https://en.wikipedia.org/wiki/Alexandria[2]o
An ancient Greek stadion was 600 greek feet, but different foot lengths were used in different Greek regions. A stadion was probably about 157.7 meters or about 172.5 yards.
https://en.wikipedia.org/wiki/Stadion_(unit)#Calculations[3]
The length of a degree of latitude is about 67 miles or 111 kilometers, which is 111,000 meters.
https://www.johndcook.com/how_big_is_a_degree.html[4]
So a latitude difference of about 7 degrees is about 7 X 111,000 meters, or about 777,000 meters. And that equals about 4,927.07 stadia.
The actual latitude difference between Alexandria and Syene is about 7 degrees and 7 minutes. A minute of latitude is one sixtieth of a degree, or about 1,850 meters. 7 minutes of latitude are about 12,950 meters, 777,000 meters plus 12,950 meters is 789,950 meters.
So the actual latitude difference between Alexandria and Syene is closer to about 789,950 meters, or about 5,009.1946 stadia, than to 4,927.07 stadia.
So maybe Eratosthenes made a lucky guesstimate, as Frigo suggests, that was rather close to the accurate distance by chance.
Or maybe Egypt was survevyed well enough that the latitude difference between Alexandria and Aswan/Cyene was known pretty accurately to people in the government survey office. Educated people who didn't work in such an office wouldn't remember the exact figure without looking it up, but would remember that it was close to a round 5,000 stadia, especially allowing for various inevitable errors in the surveys which would make more precision meaningless.