According to Islamic principles, assuming a child’s father passes away, the right to having custody of the child is, also, awarded to the mother up until the child attains the age of puberty. On occasions where the father is absent [for any given reason], such an absence does not nullify his right of custody, albeit unproblematic for the mother to take care of the child.
From a fiqhi (jurisprudential) perspective, the mother is awarded custody of the child – once the father passes away – even though the child’s grandfather is regarded as his/her automatically-ordained guardian. The latter cannot take the child from his/her mother.
Assuming the grandfather’s guardianship and custody of the child, in question, is not feasible, awarding the mother with custody will be unproblematic; however, if it is feasible, the mother ought to acquire the (paternal) grandfather’s consent (vis-à-vis the child’s custody).
The ruling vis-à-vis the father’s absence or imprisonment is on a par with his demise i.e. the mother is awarded custody albeit not regarded as her inheritance.