In reading about Church history I’ve come across several popes who were deacons. How does this work?
The Roman pontiff acquires full and supreme power in the Church when, together with episcopal consecration, he has been lawfully elected and has accepted the election. Accordingly, if he already has the episcopal character, he receives this power from the moment he accepts election to the supreme pontificate. If he does not have the episcopal character, he is immediately to be ordained bishop (canon 332 §1, Code of Canon Law , emphasis added).
Although it would be highly unusual today for a non-member of the College of Cardinals – the elective body of the papacy – to be chosen to be pope, theoretically speaking, the position is open to any Catholic man. If he is not already an ordained bishop, then he would be ordained a bishop upon his acceptance of the papacy. So, yes, a deacon – who already has the lowest degree of holy orders – could be elected pope, but he would then have to be immediately ordained to the priesthood and then to the episcopate upon his acceptance of the papacy.
Universi Dominici Gregis** by Pope John Paul II