Marshadow's entire plot was extremely confusing, but I think the entire point of Marshadow was to make sure that the Rainbow Hero's heart remains pure. When Ash started to be corrupted by his anger at his loss, Marshadow came in and showed him the vision of a world without Pokémon, causing Ash to re-realize what's really important and return to his rightful path. This would Ash was being corrupted by nothing but himself.
Honestly, I would argue this isn't even that out of character, at least for this Ash. Think about how Ash reacted to losing to Paul, a trainer with similar morals to Cross, and then think about how those reactions may be compounded by the fact that this was Ash's first ever loss. Basically, this movie had the advantage of compressing all 20 years of Ash's character development down into just over an hour, making his actions and reactions much more extreme than normal. That was one of my favorite parts of the movie, as it is often easy to forget how much Ash's character has developed over the years, since it's so spread out. Compressing it allowed us to really be reminded of how far Ash has come.