They were in prison and they prayed because they were in a difficult situation. The Bible doesn't say why they prayed at midnight, but it's likely that they did it because it was the darkest time of the night and they thought God would be more likely to hear their prayers. It's also possible that they chose to pray at midnight because it was the time when their spirits were most down. Regardless of why they chose to pray at midnight, we know that their prayers were answered.