Can Pokemon change gender when they evolve?

Solution 1:

Barring a bug, hack or you simply being mistaken, evolution never changes gender, nor does anything else.

Gender, IVs, ability (barring the ability swap item), and personality are set in stone on generation of a pokemon and are never deliberately changed by game code in any generation of Pokemon (though trading from Gen 1 to Gen 2 assigns previously genderless pokemon a gender based on personality value).

Solution 2:

Many things, such as Gender, Ability, IVs, shininess, are all determined by the Personality Value, a hidden number that's basically the Pokémon's DNA in some ways.

However, it would be wasteful to calculate all of these things every single time the information is needed, therefore the information is also stored separately from the value. This is what allows hacks like "Wonder Guard Spiritomb" to exist.

When evolution occurs, the personality value is re-evaluated. This is to allow ability changes, in cases where different stages of evolution have access to different abilities. For example, Scatterbug changes from Shield Dust or Compoundeyes, to Shed Skin when it evolves into Spewpa, and back to Shield Dust or Compoundeyes when it becomes Vivillon. Without the personality value, the ability might end up different between Scatterbug and Vivillon.

Anyway, it is possible that your Diamond game erroneously recorded your Charmeleon's gender as female, despite its personality value indicating a male. Such a glitch is, of course, highly unlikely, but I had a friend whose game allowed them to teach the Thunderbolt TM to a Vaporeon so... -shrugs-

If this was indeed the case, then X/Y would have recalculated the information based on the personality value upon evolution, thus causing your Charizard to switch genders.