The masonry layout doesn't display like the second version because of this conditional at the top of the layout function:

if ($singleImageInRow = count($PostMediaImages) == 1) {
     $PostMediaImages[] = $PostMediaImages[0];
}

The array of images is run through both array_map and a "chunk" operation to split it into pairs. If the length of the chunk is "1" it duplicates the one image and lower down in the code gets the dimensions as if there were a duplicate of that image next to it. But the total count for display is still the original count of images so it never actually displays that image twice at the end.

Comment out/remove that conditional and the last image is scaled to span the columns above it.