If Mexico had played it more as a war of attrition -as in, Santa Anna moving its capital further south, and organized the Mexican army into guerrilla units-, it would have done well. Specially with people such as Miguel Miramón being quite proficient on guerrilla and defensive warfare, as his later adventures on the Reform War can attest. Or Porfirio Díaz, who joined the army as a volunteer during the invasion, but never got to see the frontlines, I can see him really pulling off some interesting early career moves.
The American invaders were not used to the tropical diseases, and many American soldiers fell like flies because of it, and given enough time, and pissing enough people off due to disrespect to their traditions (specially the Catholics, something the Americans avoided to do OTL), the whole thing would have turned into a quite bloody stalemate, eventually getting New England to get a bit rebellious towards Washington, dragging more Irishmen (and a few Gringos, if some reports are to be believed) into desertion, and there you have, Mexico "winning" the war due to the fact the Americans are too exhausted about sending people to die in some worthless piece of land without making any more gains. Mexico would still have lost some land, but it would be less than it was OTL.
I'd say it had more to do with how the natives aren't exactly fond of the Mexican government or fighting for it, note how US marines where butchered by Mayan guerrillas during the war of Chan Santa Cruz rebellion durring our attempted intervention.
Ah, the Guerra de Castas. Though, the Mayans were never fond of anyone due to some bizarre religious beliefs they had. But that's a story for another day.