After the death, and ressurection of Jesus; before the feast Pentecost, the Apostles gathered with Peter whom Jesus had appointed as their leader. Peter spoke about the the betrayal of Judas and his death. Peter told the other Apostles it was time to replace Judas. Peter said, they should choose one of those who knew Jesus, and was with them during the time Jesus taught them. Peter reminded them, they were to elect another Apostle to help them in the work Jesus commanded them to do.
The Apostles chose, Joseph called Barsabbas, and Matthias, then they prayed for the grace to know, which man God wanted to be elected. After praying, they cast lots and the lot fell to Matthias who became the one to replace Judas.
The story of this event is found in the Acts of the Apostles Chapter 1, Verses 12-26