The live action universe is tricky, seeing how they like to play a little fast and loose with the established Transformer abilities, so for starters the closest you can get to an answer may be "because movie?".
However, the general opinion of the tfwiki is that transforming isn't responsible for Prime's "recovery", rather it was scanning/trans-scanning (the process of recording and adopting a new alternate appearance, the latter being a term from the live-action movie series).
For starters if you compare Prime's T3 and early T4 vehicle forms you notice a drastic difference; it's an entirely different style of truck, not just damaged or broken, but far closer to an English "lorry" than an American "truck". So clearly at some point Prime took on another vehicle mode since the last time we saw him.
To quote the Transformers wiki:
Though badly wounded, Prime threw off his pursuers by scanning a dilapidated cab-over truck, and fled north to Paris, Texas, where he hid in an abandoned movie theater and succumbed to stasis lock induced by his grievous injuries.
While he was badly damaged during the fight, it wasn't direct combat that caused his outward appearance per se. ("Stasis Lock" is a sleep mode, comparable to Hibernate on a PC and isn't elaborated upon very much as far as the live-action movies are concerned.)
Optimus is later woken out of stasis lock by Cade removing the missile from his system.
A later section continues:
After confirming that they had shaken their pursuers, Optimus summoned the surviving Autobots to a rendezvous point in the desert, scanning a passing truck on the way to adopt a new alternate mode and repair his injuries.
It is quite possible that Optimus' external damage was at least partially cosmetic, due to his vehicle mode. However, by scanning (strictly speaking not transforming) a new vehicle he managed to drop the battered exterior and rejuvenate.
Why wouldn't scanning the first truck have done the job? No idea. Arguably you could say that after being in stasis for so long he managed to conserve enough energy to repair himself during a trans-scan afterwards, but couldn't because he was locked into stasis, or he just couldn't do it with a live missile lodged in his chest. Scanning does (normally) require a target vehicle though, so he wouldn't be able to do it on the fly without another truck, forcing Optimus to wait until one was in range while he was capable of trans-scanning.
That said, trans-scanning (and the live action movie series) is a bit of a continuity mess, so a fair amount of this is conjecture based on what little facts were ever established.