Hello and welcome to e health forum.
Erections are very common in most young kids. They can usually start in the early years of childhood.
Some kids find them to be pleasurable, while many of them find them uncomfortable, when the erections are strong and last for a while. In such cases, since they are not accustomed to the feeling of the event, they sense that it is abnormal and complain or cry out in pain.
In younger males, mild constipation may cause intermittent erections that is not painful and resolves immediately.
In such situations, try to avoid panicking and don't let him get overly excited. You can put him in a cool bath and try to distract him, until everything returns to normal. Educating kids that this is a normal physiological reaction, either to psychological or emotional stimulation would be very helpful.
Painful erections unrelated to sexual stimulation or desire is a condition that is called priapism. Priapism, however, usually only occurs in adults, or in boys between 5-10 years old with sickle cell disease, leukemias, trauma to the penis, pelvis, or perineal area (usually due to child abuse).
Such painful erection is a true urologic emergency that may lead to permanent erectile dysfunction and penile dysfunction if left untreated. In case your kid has episodes of prolonged erections, that is painful, you might consult with a doctor for proper treatment.