(this is US-centric)
For *marriage*, most states go by birth certificate. For the purposes of marriage, in most places, your gender is what your birth certificate lists as sex. Yes, screwed up. There *is* some contradictory case law, and also case law that only recognizes the *original* birth certificate or DNA. In other words, it's all over the map legally.
There's no actual definition in law of what makes someone male or female. Seriously. It's all case law (and every possible conclusion has been reached there) or administrative rules made by an agency (these carry some of the force of law, but aren't anywhere near statutes). Perhaps they should have defined male and female before requiring one of both in a marriage. But that would have been inconvenient for the right-wingers.
As for the Feds, if your social security markers don't show heterosexuality, I can see the possibility of tax issues due to DOMA. Ironically, if you were married and filed as singles *OR* filed jointly you could be in trouble. There is no single standard of what makes you male or female at the federal government level, either.
Today, the legal situation is:
1) If DNA between the two partners doesn't include exactly one XX person and one XY person, the marriage is at risk of being legally questioned in some places.
2) If the partners' original birth sex identification doesn't include exactly one male and one female, the marriage is at risk.
3) If the partners' current birth certificate sex markers doesn't show exactly one male and one female, the marriage is at risk.
4) If any combination of similar government documents won't show exactly one male and one female, the marriage is at risk.
5) If a partner has had surgery and doesn't disclose this accurately to the other partner, the marriage is at risk.
I *THINK* that if both of you did the same things (changed certain documents, etc) at the same times, and that the equivalent documents showed a heterosexual relationship between both of you at every step of the way (so you wouldn't want DLs that show you both as M or both as F, for example), you'd be okay (as whatever standard someone used, it would show a heterosexual relationship). But the courts have a nasty habit of picking only the legal evidence of gender that they want to use - so if they could pick a standard that wouldn't show M + F between the two of you, then you could be in trouble.
I say this as one of the people who's marriage is both valid or invalid. It's sad that there is very few relationships a trans person can enter into and have a legally secure marriage. When I hear the anti-gay crowd say, "Gays can get married. They just have to marry non-gay," I want to somehow put them in a situation where they can't marry either a male or a female because some backwater judge might pick some random criteria to determine sex.
I know that's probably not what you wanted to hear. I'd encourage you to think carefully about what could happen to you if a court disagreed with you, but also to listen to your heart. This is why the fight for marriage equality is so important. It doesn't directly address bigotry towards trans people, but it side-steps it and gives rights, which is a lot better than the situation is today.