Many Mary Sues are born from attempting to force something to happen. That is, the author wants a particular plot point or character interaction to happen a certain way. Or more subtly, she needs to build a scene that shows that a character has property X.
The best way to do these is to try to develop circumstances in which the character interaction will naturally lead to what the author wants to happen. Build a scenario in which property X is naturally revealed by the characters themselves. If one tries to force things to happen against the will of characters, the invasive hand of the author can be seen.
The best way to guard against this is to have a clear character arc for each character. Know how each one responds to certain situations. And make sure that any extras or secondary characters are behaving properly. Sometimes, this means reconstructing whole scenes to make sure that the right character traits are showing through; to help with this, try to "storyboard" the plot before actually writing it. Make rough ideas of what the arc should/will entail, and use those to help write the characters to suit the plot without ham fisting them in.
The easiest example to show how forcing creates Mary Sues is with a Villain Sue. Accidental Villain Sues are commonly created by well-intentioned attempts to show that certain villains are credible threats to the heroes. The audience may accept one or two contrived circumstances or instances of hyper-competent behavior that allow the villain to pose a threat. But if it happens repeatedly, in different ways and circumstances, a Villain Sue results.
Attempting to make a Xanatos Gambit, for example. A Xanatos Gambit seems quite simple: there are a limited number of outcomes and all of them are desirable to the planner. But the more possible outcomes there are, the more contrived a gambit becomes, the more the author's hand is seen, and the more likely the audience is to see the character as a Mary Sue.
YOU ARE READING
How to not write a Mary Sue
RandomA Mary Sue is an idealized and seemingly perfect fictional character. These characters are often irritating and cringe worthy. This book will tell everything about identifying a Mary Sue and how to avoid them.