I couldn't get a good explanation from a native speaker, because for them it's all super obvious for them, hence, unexplanable. But over time, I think, I've got it.
First, the idea is that Y drastically changes the meaning of X. Y refers to a different context, in which X becomes funny. In most cases it's sexual context, as if X was said by a woman during sex. Examples:
- Man A "Pull it out slowly."
- Man B "That's what she said!"
(See more in urban dictionary)
I didn't get it after first explanation, I got it but didn't find it funny after some more explanations, and now, after five years in U.S., it kinda makes sense and I can say it's funny :) I'll try to think why this trick may not be intuitive to comprehend for Russians:
1. First, this principle is used only occasionally in Russian language, it's not as widely used. In conversation when one is making fun of what other has said, it's more common that he'd change content instead of change context, or continue conversation in a funny logical sequence. For example, "В этом году я сдам сессию и перейду на следующий курс" - "А я стану балериной и выйду замуж за миллиардера" - здесь вторая реплика намекает на то, что первая маловероятна, и из маловероятного следует маловероятное. Второе очевидно маловероятное, поэтому становится смешно, понимая сделанную логическую связь и понимая, что собеседник смеётся над первой репликой.
2. In Russian language it's rare when context is changed drastically or inverted. It's more common that context changed slightly, with a few added or removed elements, just to add a sarcastic note.
3. In English you use "she" regardless of gender of the first person, who said "A". Often it is a male. In Russian saying "you're a female" to a male in funny context is usually very insulting. It may imply assigning him female stupidity (like saying "you're dumb"), irrationalism (another way of saying "you're dumb"), weakness ("you're not strong enough"), lack of male sexual strength ("impotent") or vulnerability (with offensive connotation, like "we'll treat you as a bitch"). In English gender stereotypes are much weaker, and you won't think about these things. Second, when listening to "B", in your mind, you'll picture *another woman* as if she said "A". They won't imply linking male personality to female. So you'll likely get the joke and laugh it off, treating it as a small funny distraction, instead of feeling personally offended. It's a kind of a different way of making abstractions, when illustrating something in a conversation.
4. In English "B" doesn't necessarily imply that "A" is wrong or not trustworthy. "B"'s function may be only a funny distraction. While in Russian most of this kind of jokes are just a way of saying "I don't believe you" or "A is wrong".
Update. Got another example of jokes with similar structure (thanks sim0nsays for link):