Are all of the following sentences OK?
I have two children with my ex-husband.
I have two children from my ex-husband.
I have two children by my ex-husband.
Do they mean the same thing? Is any of the variants to be preferred over the others?
Purely intuitively, I would say that the third would be most frequently found, that the first requires the past tense (I had two children with my ex-husband) and that the second leaves open the possibility that the speaker is not the children's mother.
Personally I don't think any of them really read quite right. Perhaps a man might say:
I have two children by my ex-wife.
...but saying it the other way round seems to imply that the man gave birth to the children.
You'd probably be better to say:
I had two children with my ex-husband.
But the more usual way to express this would be:
I have two children from my first marriage
I think it depends on whether you are trying to indicate a birth relationship or a parenting relationship.
The phrase by my first husband/wife suggests that you and your spouse are the birth parents of the child in question.
The phrase with my first wife/husband, could mean you are both birth parents or, if you adopted the child, that you did so together.
The phrase from my first marriage (as has been suggested by several others) may reflect a birth relationship, but just as easily may mean an adoptive relationship or even a step-parent relationship. Many ex-spouses consider the children who came to their family through a former spouse to continue to be their children, even if they are not the birth parents and never formally adopted.
The phrase from my first wife/husband similarly could be any of the above.
The sentences would each be more proper as follows:
I had two children with my ex-husband.
I have two children from a previous marriage.
I have two children by my ex-husband.
In the first case, because you (the speaker) yourself are no longer with your ex-husband (by definition if not intuition) then you bore two children while you were with him, so past tense. You still "have" those children in terms of their being alive and you being their mother, but the meaning of "to have" changes slightly to refer to the act of making and giving birth to them.
In the second case, while the way you said it is technically correct (your ex-husband "gave" you those children in some sense, so they're "from" him), it's much more common to hear "from" in terms of a relationship, such as the marriage between you and your ex.
The third case is correct as you said it. It may sound odd but it's perfectly fine.