It's safe to say Mexico lost the war before the war even started. But the US wasn't anywhere near the superpower status it is today even accounting for technological realities of the modern era, you don't need divine intervention to beat it. Just ask the Canadians.
Mexican artillery was severely hampered by the bad gunpowder as well as the ability of Mexican troops to shoot. The bad gunpowder forced troops to fire from the hips, which meant very few shots would ever hit their intended target. This alone could have caused a few battles to swing in Mexico's favor. Having a better president who knew how to trust his generals and not get in their way would have also been a bit more helpful, so let's pretend Santa Ana died during the pastry war when he lost his leg in the OTL. A few early victories, maybe keeping US troops north of the Rio Grande, would have gone a long way to ramp up anti-War sentiments...think Vietnam war but a hundred or so years earlier.
Eventually the US will push down south and its navy will land troops in California as it did in the OTL. Then there will be troops landing in Veracruz, maybe even earlier in the war due to the difficulty of invading from Texas. If Monterrey can beat back an American army once (or if they get lucky twice) then there is bound to be a loss for US troops in their martch to Mexico City from Veracruz, maybe a 5 de Mayo style victory for Mexico. This is enough for Mexico to sue for peace and negotiate. It's not a Mexican victory, but it wouldn't be a Mexican defeat either, and Mexico would actually be able to have a realistic say on the terms of the ensuing peace treaty.
Here's a small timeline I can think off with a POD that tries to deal with some of the problems Mexico had.
- Santa Ana dies, he claimed IOTL to be very sick and therefore retired from the presidency...what if he was sick and died in 1835?
- Texas still rebels under the liberal presidency of Valentin Gomez who manages to use the moment to rally support but a conservative coup attempt makes it possible for Texas to get independence.
- Gomez survives the coup attempt and uses that as an excuse to act against the centralist constitution restoring aspects of the federalist government, thus the states are now happy, especially places far away from Mexico City.
- Military reforms means maybe buying/producing slightly better gunpowder
- Miguel Barragan becomes president in 1837 and is a more moderate (OTL he was a moderately liberal VP chosen by Santa Ana to run things while he went to Mar-O-Lago...sorry, couldn't resist).
- Pastry War in 1838, maybe Barragan's government is able to negotiate something? Maybe butterflies changed the aspect of the conflict so that it was a bit more limited? In any case, this ruins OTL and TTL's reconquest plans for Texas.
- 1841-1845 A new president takes power, assuming we can keep conservatives at bay? Maybe a moderately conservative maybe deals with a few minor coup attempts that keeps the Mexican army busy.
- April 1845 A new president takes over. It doesn't really matter if it's a liberal or a conservative but they get into the Mexican American war before they make another state revolt.
So Mexico of this timeline has a few less revolts, a more united homefront, better gunpowder thus better guns and accuracy, a less than slezzy buthole of a president, and generals who will not be limited by a gloryhound. This allows general Pedro de Ampudia to defeat Zachary Taylor's initial advance into Monterrey (which could have been possible in the OTL) in September 1846. In the OTL Taylor's armistice with Ampudia was not very well received back in Washington DC. So in TTL it may have been a very bad mark on him. Not to mention this would have been fuel for the anti-war crowd. In the OTL, the follow up battle of Buena Vista could have easily gone in the other direction if not for Santa Ana's blunders and if Mexico had effective guns. So let's say a second round led to the capture of Monterey but then the subsequent battle at Buena Vista was a Mexican victory in Feb 1846 (also OTL date). This allowed Mexican forces to lay siege on Saltillo...maybe even push back the US into Texas. The battle of Sacramento River was one where things could have conceivably gone differently thus preventing Chihuahua from falling and keeping Zachary Taylor from getting reinforcements forcint Americans back north to Santa Fe.
The Pacific blockades would still happen the same ITTL as IOTL so the US wins California. Maybe some ports would beat back American forces? But the pacific would belong to the US due to Mexico's lack of a Pacific fleet capable of defending those ports. Enter Scott's invasion through Veracruz. If the past two Mexican presidents played their cards right, it would be harder for Scott to march through Mexico due to local resistance especially if his supply lines get cut. All it takes to push him back to Veracruz is a 5 de Mayo style defeat maybe during the battle of Japala where premature firing of artillery doesn't give Mexican positions away? In any case a prolonged campaign of a mixture of victories and defeats and getting cut off from Veracruz forces Scott to retreat to Veracruz by fall of 1847.
With fierce anti-war sentiments in the US, several defeats in northeastern and central Mexico, and accusations of a pro-slavery land grab becoming increasingly popular in the North, and Scotts army back in Veracruz with their tails between their legs Mexico sues for peace and the US accepts.
A less drastic cession happens. Maybe the US/Mexico border is set on the Nauces river and mexico keeps everything south of San Jose and for a more fair price too. It's not a victory but it's not a defeat either. For a victory you'd need an earlier POD. Give Mexico a better navy, more stability, non-US based immigration in northern territories, a more stable government. Nothing too big, and Mexico could keep the US from doing anything beyond blocading the Pacific and Veracruz and invading California.