Same way everything evolves!Primarily due to natual selection,(basically survival of the fittest.Strong gentic traits will survive over weak ones and vice versa.Also mutations can occur in genes and casue new traits and if they are useful then they will prevail over weaker traits)gentic drift(basically the pure randomness of life-whether an organism and thus its genetic material survives can be pretty random sometimes and so genetic material that is not necassrily the srongest can survive)and mendelian inheritance (a species will transmit its genetic material to its offspring via reproduction).Jeees thats is strange. How would a creature evolve to do something like that?
Modern evolutional theory FTW!