If Ukraine abandoned Bahkmut, the Russians would have attacked some place further west and perhaps at one or more locations that were more strategic and threatening to Ukraine.
Wagner was the most effective fighting force that Russia had. It has now been largely decimated.
Ukraine's counter-offensive has needed time to marshall its forces (and train its troops to use Western equipment). The fight in Bahkmut bought that time.
I believe that the Ukrainians who fought and died there will prove to be the heroes who saved Ukraine.