MX didnt turn himself into a Heartless. He released his heart and put it in Terra's body. His body was gone, it died.Also, he was already dying anyway hence why he needed a new vessel.
But somehow vanquishing Ansem and Xemnas caused his old body, the one that was dying, to reunite with his heart. And he acts that he knew this fro before.
Well, if he could cheat death why did he possess Terra in the first place?
So, first of all, MX didn't "die" when he possessed Terra. His body merely disappeared. You only "die" in the KH universe if both your heart and your body are destroyed together. Otherwise, you become a different type of entity, unless you have a different host body in which to dwell like MX and Ventus did.
When Terra-Xehanort split into a Heartless (Ansem) and Nobody (Xemnas), the body of Terra-Xehanort became Xemnas. This means that MX's and Terra's hearts both became Heartless together, and MX continued to assert the same control over Terra's heart as he had before, because Terra's mind at this time was trapped inside the Lingering Will in the Keyblade Graveyard. This is also why you see Ansem with the Guardian in KH1, because MX's heart—which uses Riku's body to take corporeal form as Ansem SoD—still maintains control over Terra's heart, which manifests in the form of Ansem's subservient Guardian.
Now, if you have a person who's been split into a Heartless and a Nobody, there are two things you must do to restore them to their original selves. First, you must eliminate their Heartless. After all, a Heartless is basically just a creature of pure darkness that consumes and holds hearts. Therefore, destroying a Heartless releases the captive heart inside of the darkness. Second, you must destroy the person's Nobody. A person's Nobody is basically a person's mind and soul operating together without the original person's heart. Over time, this Nobody will grow a new heart as it gains more experiences. Destroying a Nobody releases the original person's soul in the same way that destroying their Heartless releases their heart.
If you destroy the Nobody, and if the person's heart is freed from its Heartless prison, then the heart will basically rejoin with the soul of the vanquished Nobody, and together these two elements of the person will recreate their body anew. And that body will take whatever form the person was before they became a Heartless.
Now, with regard to MX, this is where things get a little wonky. You see, beings like Terra-Xehanort—entities with two hearts inside of them—are extremely atypical. One might even consider them a sort of abomination. In a sense, Terra-Xehanort can be considered his own individual person. Ergo, when he splits into a Heartless and Nobody, reformation works differently, and both MX and Terra regain their bodies in different ways.
You see, Ansem being vanquished released BOTH MX's and Terra's hearts at the same time. Xemnas being vanquished released both MX's and Terra's souls. MX's mind joined his heart in becoming a Heartless because he became one by choice, which is extremely atypical. Terra's mind, however, remained in the LW.
So, you have four components to complete a human being: heart, body, soul, and mind. Heart + soul = body. But without either mind or heart, that body remains in a kind of comatose state. So, when Ansem and Xemnas both died, MX's heart, soul, and mind were free to come together and recreate his corporeal form. The reason he reincarnated as his older self, is because that was the last form he existed in AS MASTER XEHANORT. As Terra-Xehanort, he was a completely different person. At this time, Terra's heart and soul both recreated his original body as well—the body he had right before becoming Terra-Xehanort, the last body he had AS TERRA.
But since Terra's mind remained trapped in his armor, that left Xehanort the opportunity to use Terra's body as a vessel for his heart. Specifically, he used it to house the heart of Terra-Xehanort from BBS, which Xehanort had his younger self travel back and retrieve from that era. It was only when TX's Guardian destroyed the Lingering Will that Terra's mind was free to enter into the Guardian, which is why Terra was finally able to regain control over the Guardian during the battle at the KG, because at that point his heart, body, soul, and mind were all in the same place at last.
This was a bit of an essay, but hopefully it made things a bit clearer.