"While Peter was below in the courtyard, one of the servant girls of the high priest came by. When she saw Peter warming himself, she looked closely at him. 'You also were with that Nazarene, Jesus,' she said. But he denied it. 'I don't know or understand what you're talking about,' he said, and went out into the entryway." NIV translation
When a servant girl tried to identify Peter as a disciple of Jesus, he denied it and moved away towards the entrance to the courtyard. We might be quick to judge Peter's actions but we must remember that he was afraid and did not know what was going to happen.
"When the servant girl saw him there, she said again to those standing around, 'This fellow is one of them.' Again he denied it. After a little while, those standing near said to Peter, 'Surely you are one of them, for you are a Galilean.' He began to call down curses, and he swore to them, 'I don't know this man you're talking about.'" NIV translation
Two more times Peter denies his relationship with Jesus even though it was obvious to everyone there that he was a disciple. He was, in fact, a Galilean and they would have had a different accent than the Judeans that were in the courtyard. Because they could tell that he was a Galilean, it was only reasonable that he was a disciple.
"Immediately the rooster crowed the second time. Then Peter remembered the word Jesus had spoken to him: 'Before the rooster crows twice you will disown me three times.' And he broke down and wept." NIV translation
Peter remembered that Jesus had predicted Peter's denial of Him and it finally broke him to the point where he cried. We know that God uses everything for good so we might ask why all of this was necessary. First and foremost is the fact that all of the prophecies concerning the Messiah had to be fulfilled including those concerning His rejection by all. Another possible reason is that the church was going to be built by this man and that could give anyone a big ego but Peter would always remember the fact that he was still just a man who had denied Jesus three times. This experience would serve to keep the man humble.