The same thing happened to my son and me. He was 3 at the time, though. We were at Staples, and one of the employees there said it to a fellow employee while we were standing at the checkout. It's funny because as soon as he said it, without a beat, I blurted out "Watch the mouth!". He was so embarrassed. But given your situation, since these kids weren't at worked, the outcome, I'm sure would not have been the same!
At any rate, my son said the infamous word in the parking lot and when we got home. I explained that this is a word that is not said and isn't tolerated. He said it again, and I just got up and left the room. He said it again while we were playing together, and I again just got up and left the room without saying anything. I came back later and he asked me where I went. I said I left because I didn't like being around him when he speaks 'like that'. He has only said it 2 more times since, and each time I just left (and made sure he saw me do it). Then he forgot about it.
Good luck! I think your approach will work! I would also make sure any other caregivers act consistently with your approach, too. Give it a little time. I'm sure he'll forget, too!