Adding members to a group dynamically in Drupal 8
So, modifying the code a little bit and saving the group after adding member to it worked for me. Pasting the updated code below:

use Drupal\Core\Entity\ContentEntityBase;
$account = \Drupal\user\Entity\User::load(\Drupal::currentUser()-&gt;id());
$group = \Drupal\group\Entity\Group::load($group_id);
$group-&gt;addMember($account);
$group-&gt;save();