The number three makes for good storytelling. There are lots of threes in this sequence.
In Gethsemane, Jesus prayed three times while Peter slept three times. Jesus had told Peter to watch and pray so that he would be able to stand in temptation. Peter slept instead.
Now he denied Jesus three times.
There seems to have been no threat against the disciples, but I suppose Peter did not know that. And he was following Jesus, if at a distance. He at least wanted to know what happened. Probably at this time he would have been resigned to Jesus dying.
Matthew’s original audience knew plenty of informal persecution for being Christians. And that’s what Peter is experiencing here. I mean, he is only dealing with recognition by a couple servant girls and some bystanders.
His denials increase in amplitude. The first is almost more of a misdirection. In the second he uses an oath, contrary to the instructions of his master. In the third, he calls down a curse. He may have even been cursing Jesus. Matthew isn’t specific, and most English translations are reluctant to go there.
He said he did not know Jesus. Imagine Jesus saying, “I never knew you.” Ouch.
Then the rooster crowed and Peter realized what he had done. He remembered Jesus’ words even if he denied knowing him. He left that place and wept bitterly. I’ve wept bitterly before and so have you. This was the right thing for Peter to do.
Peter was repentant for what he had done. It is implied that he was forgiven by Jesus and restored to his relationship with him.
Judas is a different case.