This is done by the "Gallery Grid" Shortcode when you select "masonry" Grid layout!
t2 Caption
Caption 3
p4 Caption
a5 Caption
Caption for 1