How to remove a container disk and reclaim space with main partition?

I used to have a bootcamp partition but somehow while attempting to remove it its changed into a container disk. (don't know how this happened, I didn't explicitly create a container).

enter image description here

enter image description here

diskutil list:

/dev/disk0 (internal):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                         500.3 GB   disk0
   1:                        EFI EFI                     314.6 MB   disk0s1
   2:                 Apple_APFS Container disk1         402.0 GB   disk0s2
   3:                 Apple_APFS Container disk2         97.8 GB    disk0s3
   4:                 Apple_Boot Boot OS X               134.2 MB   disk0s4

/dev/disk1 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +402.0 GB   disk1
                                 Physical Store disk0s2
   1:                APFS Volume Macintosh HD            336.1 GB   disk1s1
   2:                APFS Volume Preboot                 23.2 MB    disk1s2
   3:                APFS Volume Recovery                515.0 MB   disk1s3
   4:                APFS Volume VM                      2.1 GB     disk1s4

/dev/disk2 (synthesized):
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      APFS Container Scheme -                      +97.8 GB    disk2
                                 Physical Store disk0s3

How can I delete this and merge its space back into the main drive?


Here, I assume the macOS volume that you boot to is Macintosh HD.

The commands below should remove the extra partitions and return the free space back to macOS.

 diskutil erasevolume free none disk0s4 
 diskutil apfs deletecontainer disk0s3 Untitled
 diskutil erasevolume free none disk0s3
 diskutil apfs resizecontainer disk0s2 0

If you are using Big Sur or newer, then you can just enter the commands given below.

 diskutil erasevolume free none disk0s4 
 diskutil apfs deletecontainer disk0s3
 diskutil apfs resizecontainer disk0s2 0