How did John the Baptist escape Herod's massacre of the infants?
The short answer
Scripture does not say. But Herod apparently limited the killings to the area around Bethlehem, where the Messiah was to be born, and John's family seems to have lived elsewhere, in a town in the hill country of Judah, perhaps far enough away to protect him.
Scripture does not say. Herod apparently confined the killings to the region (Matthew 2:16) where he knew the Messiah was to be born (Matthew 2:1-6).
As far as we know, John and his parents did not live in or near Bethlehem; otherwise Mary and Joseph could have stayed with them during the enrollment (Luke 2:4-7). They lived in an unnamed town in the hill country of Judah (Luke 1:39-40), which may have been far enough from Herod's attack to protect the infant John.