Did Socrates truly deserved to die? Yes and no, depending on the perspective.
On the one hand, he was punished according to the laws of his state; laws whose value he believed wholeheartedly. He so much appreciated the importance of the law, he has put it above his own life. According to social norms, it was good that he sacrificed himself in the name of the state and its order. Even to this day those who sacrifice themselves for their state are considered heroes. It is as if society wants people to die for it, if their deaths contribute to it. Sometimes I wonder if society actually prefers its own sake and benefit over those of its members.
On the other hand - what did he just did, after all? Teach philosophy to the citizenry of Athens? Encouraging them to practice their own logical proficiency? Examine their lives? Broaden their horizons? Are these deeds truly corrupt, as was his accusation? Why are these deeds corrupt? Because they shake the mental foundations of society? Because they encourage people to think for themselves, instead of blindfully obeying the state and its commands?
This is a core reason why I abstain from having a full intervention in society, and instead live a relatively solitary life - society is egocentric. It mostly cares for itself. It glorifies the death of its citizenry and soldiers if their agony promotes its interests. It includes inside itself anything and anyone who benefit it and show loyalty and patriotism to it, and rejects every data or every person which threatens its pride and collective conformity.
Socrates’s punishment is a lesson for us all about the two-faced society (society as a general term) - it encourages us to contribute to it and praises us for doing so, and makes us feel ashamed of ourselves when we go against it and its values, in the name of our independence. If society is that manipulative in order to make us its mental servants, away from mental independence - why, I ask - why fully intervene in a construct that cares more about its own pride and puts you to shame if you are to follow your own ego? The hypocrisy of society is that it downgrades egoism as corrupt and the source of evil, but, at the same time, glorifies its own ego, and makes you believe that feeding its ego (in the name of altruism) is an exalted thing to do and to dedicate your entire life for it.
Socrates wouldn’t be punished if he was to submit to conformity that the authority imposes upon its people. Why would he be executed if his existence is to contribute to society? He would have medals and his name glorifies in the streets, because everyone that glorifies society, becomes glorified and exalted themselves in the eye of the social consciousness. His death shows the ugly side of social constructs, as beings that manipulate you to give up your independence entirely for society, because your egoist and eccentric independence from the authority of the norm, allegedly, should be “condemned”, just because you go in a way the social norms refuse to dictate.
So no. Socrates needn’t to die; the social consciousness needed, if the authorities were altruistic towards his unique potential, instead of being egoistic and authoritarian in the name of their control over the stability of their subjects.