The teacher probably realised after he said, 'you could be stealing things', that you were black and that the context could be misread as him being racist. That's probably why he went quiet, he was worried. Teachers always try to keep students out of locked areas, if something were stolen, they could be liable.
I doubt he would have purposely racially attacked you. Most likely an unfortunate coincidence.