During upgrade groups and members are transferred from old tables 'mdl_groups_temp', 'mdl_groups_members_temp'. In the former case the records aren't sorted on ID and the group ID may be lost in the function 'groups_create_group' - the ID is auto-incremented, so a new one is used.
Note, may be database dependent.