A lot of locomotives on a lot of railroads have been wrecked and repaired. That was one of the jobs given to the large backshops, which were filled with the proper equipment and the skilled employees to do this work. In most cases, nobody seeing or running the repaired locomotive would know.
In the diesel era, EMD repaired a small number of wrecked engines, and Alco also did a smaller number. That was the owning railroad's decision, based on a number of factors, but they could have made their own repairs if they had desired to do so. In the steam era, there was no such option, and many steam engines were repaired and returned to service after serious wrecks.