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