Why did the midwife tie a red thread on the hand of the fetus that poked out?
Seforno (in Pasuk 27): Because already before they were born, one could tell that they were twins. 1
Moshav Zekenim, citing R. Yehudah ha'Chasid: Even though in any case, he is not [Yehudah's] Bechor, the custom was that the Bechor of a second wife received a field designated in her Kesuvah, 2 for an extra portion. Also, it is relevant to redeem him from a Kohen for five Shekalim, which depends on Peter Rechem. Twins are prone to get mixed up. 3
Perhaps she suspected that it was a Divine Siman that he stuck out his hand, just like Yakov came out holding Esav's heel (refer to 26:25:1:2). The Bechor was called Peretz because he overtook the one who stuck out his hand (PF)!
This is like the opinion that Yehudah kept Tamar for a wife (refer to 28:26:2:1,2). At the time of Bi'ah, even if he was Mekadesh her, he could not write a Kesuvah without knowing her name! (PF)
Peter Rechem depends on birth, i.e. when the head or majority leaves. It would have sufficed to make a Siman on one baby after full birth! (PF)
רש"י: הוציא האחד ידו: מנין לפרש כך?
גור אריה: אין לפרש מלשון נתינה ממש שהרי לא הייתה לו כוונה להושיט יד.
רש"י: ולאחר שקשרה על ידו השני החזירה: מנין למדו זאת?
גור אריה: כל עובר מוציא איבריו החוצה בשעת הלידה, ולמה כתוב שהוא הוציא יד? אלא שהוא הוציא יד והחזיר ולא הייתה כאן לידה ממש.