I only remember the details because I've actually seen most of the related episodes recently. Barney and Marshall made the original slap bet way back in "Slap Bet" regarding the mystery of Robin's aversion to malls. At the end of the episode Marshall was granted five slaps on Barney at his discretion. He used up four of them and then was granted three extra slaps in Season 7 in exchange for Barney being allowed to take off the ducky tie he'd been forced to wear after losing another bet. Marshall used two of his remaining four right away, leaving him with two more, one of which he finally used in this episode. That leaves one final slap, which I'm sure they'll get to by season's end.
As to why Marshall was slapping Barney this time, at the end of the last episode he apologized to Barney for missing the rehearsal dinner and said he got Barney a gift to make up for it. Barney, being Barney, obnoxiously demanded Marshall give it to him immediately, thus incurring the slap.
One of the things I'll miss most about this show is its surprisingly tight continuity (for a sitcom). Although both Barney and Marshall referred to this latest slap as the fourth one when it was actually the seventh, so maybe it's not so tight after all.