As part of the process of applying for a US trademark, an applicant (or the applicant's agent) must do a search for identical or aimilar marks in current use. The existancen of a sufficiently similar mark in the same or a similar category can be grounds for refusing the application, Also a proposed or recently registered mark can be challenged by the user of an identical or similar mark that was in use before the challanged mark .
Thus if one seeks to register a mark in previous but unregistered use, the application might be refused or challenged.
After several years (I think five) a registered mark is no longer subject to challenge for previous use. However, if a newer user attempts to move into an area where the same mark is already in use, and has been before the newer mark ws in use, it may not be able to.
Procedural details will be different outside the US.