Returning data instead of echoing/printing

The problem with your code, as you mentioned, is that you are outputting the images via your print_r function, and you need to instead build a string to return, which is displayed in place of the shortcode.

Give this a try:

function get_media () {
    $attachments = get_posts([
            'post_type'   => 'attachment',
            'numberposts' => -1,
            'post_status' => null,
            'post_parent' => get_the_ID()
    ]);
    
    if ($attachments) {
        foreach ($attachments as $attachment) {
            $output .= wp_get_attachment_image($attachment->ID);
        }
    }

    return $output;
}

add_shortcode( 'media', 'get_media' ) ;