Your shortcode function callback must return the output, like this:
function gamma_gallery( $attr ) {
$html="";
// your stuff, where all the output is assigned to the $html variable
return $html;
}
or you could try the output buffering:
function gamma_gallery( $attr ) {
$html="";
ob_start();
// your stuff
$html = ob_get_contents();
ob_end_clean();
return $html;
}