If a minor has been taken into custody under this article and not released to a parent or guardian, the juvenile court shall hold a hearing (which shall be referred to as a “detention hearing”) to determine whether the minor shall be further detained. This hearing shall be held as soon as possible, but in any event before the expiration of the next judicial day after a petition to declare the minor a dependent child has been filed. If the hearing is not held within the period prescribed by this section, the minor shall be released from custody.