"When the time came for her to give birth, there were twin boys in her womb. As she was giving birth, one of them put out his hand; so the midwife took a scarlet thread and tied it on his wrist and said, “This one came out first.” But when he drew back his hand, his brother came out, and she said, “So this is how you have broken out!” And he was named Perez. Then his brother, who had the scarlet thread on his wrist, came out. And he was named Zerah." - Genesis 38: 27-30
It is implied that before birth, they knew the mother had twins.
Any good midwife can determine when a woman is carrying twins. The uterus will be bigger, and there will be excess everything: excess heads, excess rumps, excess backbones (these are things that can actually be felt and located on manual examination). Late in the pregnancy, if both twins are head down (the usual and most common presentation), there will be two rumps up top.
The first thing to come out of the womb was one baby's arm... The arm goes back into the womb.
In my medical experience delivering babies (I've delivered a significant number), this is fairly unlikely if the story is told without including pauses. For one thing, the waters would have had to have broken - at least one sac if they were dichorionic diamniotic twins, which is most common for non-identical twins (which they were). This usually occurs very close to the time of birth, when the pelvis would be already occupied by a head. There's not a lot of room for hands and arms to be swinging freely in there.

The first thing to come out of the womb was one baby's arm (upon which the first thing the midwife does is tie a crimson thread to it).
However, compound presentations may occur in a "roomy" pelvis, and are more likely to occur when the pelvis is not fully occupied by the fetus because of low birth weight, multiple gestation, polyhydramnios, or a large pelvis. They occur in singleton pregnancies about once in ~1500 births, and usually, the "misplaced" part is a hand or arm. In general, if left unattended, the hand will retract or the arm will extend further as labor progresses.
...unborn children have all their reflexes in utero, [therefore] unborn babies are fully capable, within the limitations of the space available, of reacting as they would as newborns. The simplest approach, therefore, may be to apply a benign noxious stimulus, such as a gentle pinch to a fingertip of the advancing hand. By applying a benign noxious stimulus (between contractions, of course), the hand may withdraw and never appear in the undesired position again.
In Genesis, note that the hand alone came out, and since it usually retracts, this is possible. The tying of thread around the infant's arm might be enough noxious stimuli to cause it to retract.
As I said, with twins there's less room, however
Rupture of membranes when the presenting part is still high also increases the risk of compound presentation, cord prolapse, or both... In multiple gestations, a possible scenario involves the head of the first twin and an extremity of the second twin within the birth canal.
Which would be the exact scenario described in Genesis.
Dichorionic diamniotic twin pregnancy
Image from About Multiple Birth Pregnancy
Compound Presentations
Management of the fetus with compound presentation