We do not know. First, Syene and Alexandria are not on the same meridian, Alexandria is about 3° to the West, and second, Syene is not on the tropic (where the Sun is straight up on the summer solstice) either, which is another assumption Eratosthenes makes, according to Cleomedes.
Our primary source on Eratosthenes's Geographica, where these assumptions were, presumably, justified, is, unfortunately, Strabo, whose mastery of geometry leaves much to be desired. Here is what Russo writes in Ptolemy's longitudes and Eratosthenes' measurement of the Earth's Circumference on his account:
"Strabo writes in a couple of passages that according to Eratosthenes the Nile flows along the meridian from Syene to Alexandria, but in the same passages the Nile is described as flowing along the same meridian even from Meroë to Syene, while in the book devoted to Egypt, still quoting Eratosthenes, he describes the path of the Nile as far from being a north-south straight line and in more than one instance Strabo appears to confuse distances with their orthogonal projections along a meridian."
If we take Strabo at face value, Eratosthenes could have taken the word of mouth that sailing down the Nile was northward, which could be roughly determined by watching the Sun and the Polaris. Just as he took the word of mouth that the Sun was straight up in Syene on the summer solstice.
But even Shcheglov, who disagrees with Russo on Eratosthenes, is equally unkind to Strabo, see his Hipparchus on the Latitude of Southern India. And the above does not align well with Eratosthenes's reputation as a meticulous geometer and geographer, who, as the head of Alexandrian library, had a corps of surveyors at his disposal. In the Forgotten Revolution Russo questions the accuracy of Cleomedes's account:
"Do we know for sure that Eratosthenes assumed that Alexandria and Syene lay on the same meridian and that Syene was on the tropic? The primary source on the subject, Cleomedes, actually wrote:
Eratosthenes' method, being geometric, seems more difcult [than
the previously explained method of Posidonius]. What he says will
become clearer if we allow ourselves to make two assumptions. We
assume rst that Alexandria and Syene are on the same meridian.
Cleomedes does not give a detailed account of the method, that would be pointless, Eratosthenes' work still being available, but a pedagogical précis meant for readers scared of the complex geometric arguments of the original work. He attains his goal of explaining Eratosthenes' method by taking an ideal case obtained by eliminating all technical difficulties; how else could he compress into three pages a work that occupied two books? Cleomedes also rounds off the numbers, evidently so as not to bother the reader with calculations inessential to an understanding of the method... Cleomedes and other authors probably name Syene because it was the Egyptian town closest to the tropic and the most convenient base for an expedition to the tropic. As for the well whose bottom was lit by the sun at the solstice, Pliny says that it was dug out for a demonstration."
Eratosthenes could have his surveyors do a version of dead reckoning, i.e. track the angles with the North and the distances traveled in the meandering of the Nile, to determine the longitude more accurately, but his map does draw a meridian through Syene and Alexandria. Then again, this map comes down to us through none other than Strabo, see The Eratosthenes-Strabo Nile Map by Rawlins.
We do know that a century later, in a treatise characteristically titled Against the ‘Geography’ of Eratosthenes (see Analysis of the latitudinal data of Eratosthenes and Hipparchus by Marx on related issues), Hipparchus proposed an astronomical method for measuring longitudes. It relies on comparing absolute and local times, with the former measured by marking when a lunar eclipse began and ended with an accurate clock.
Ptolemy implied that he used this method to determine the longitudes of multiple locations in his Geographike Syntaxis, but his data suggests that he used Roman itineraries (road distance measurements) instead, along with a widely off value of Earth's circumference. The idea will be vigorously pursued in the early modern times, when what came to be known as the problem of longitude was ultimately solved by designing chronometers, see also When was the issue of time zones at different longitudes first described? However, those were not available in antiquity.
Russo's alternative speculation runs as follows
"Fixing longitude has always been much harder, until the advent of chronometers and radio, and we don't know what method Eratosthenes might have employed. Possibly it was the one mentioned by Ptolemy at the beginning of his work, whereby one finds the difference in longitude between two places by determining the difference in latitude and estimating the angle that the line between the two makes with the meridian. In the case of cities linked by a sea to the West lane, this information would have been known approximately to seafarers
plying the route."
As I mentioned, Russo's speculations are not without detractors, see e.g. Shcheglov's Accuracy of Ancient Cartography Reassessed for a much more skeptical view of Eratosthenes's work.