When my child was in first grade she had a terrific teacher with 25 years of experience. Had this happened with two boys in her class, that teacher would have known right away to ensure they were kept apart when possible -- not sent together as "bathroom buddies" or on errands to other classrooms, if the school has a kid-must-have-buddy rule to be outside class. Not paired on activities or projects. Certainly not at desks or seats next to each other. On opposite sides of the group if the class is all sitting on the floor for a story or talk. And watched with care on the playground. Going over to a fence while on the playground probably wouldn't have happened; teachers who were on the playground tended to post themselves between kids and things like fences or areas with tempting gravel, so the kids stayed focused on the play equipment.
This teacher, being experienced, managed to do this kind of thing without making a big deal about it or being obvious or announcing, "Billy and Sammy must be separated!" She knew that drawing attention to it that way would make Billy and Sammy just wild to BE nearer each other.
Mom, this will pass and as Marda noted below, this isn't as bad as it seems to you it is, and it's normal at this age for many kids. I would talk with the teacher (in person, without your son there) briefly and say that while your son has to take full responsibility for his own behaviors, it would be a good idea if he and Billy are kept apart. (I kind of wonder how two kids got into an empty classroom alone, by the way--? That shouldn't be able to happen at this age.)
Be clear with the teacher that that although your son is on the spectrum he should be given the same discipline, as swiftly, as any other kid (because...shouldn't he, in these cases?). But also let the teacher know that he will imitate anyone doing anything impulsive, so you need to enlist some attention for keeping him away from a kid whom he clearly wants to impress. The teacher will not do it perfectly all the time, nor should you expect that of her or him. But you need to make an ally of the teacher in this while also showing that you are not expecting some kind of special leniency for your son, only some attention to keeping him occupied and not pairing him with Billy for anything.
Does that make sense?
Also, if he is on the spectrum, is the school counselor aware and involved? I am not saying to bring in the counselor over classroom behavior issues like the ones you describe, but it might help if the counselor knows that he tends to imitate other kids. The counselor might be able to work with him on that tendency over time.
Are you getting reports on these things the same day they happen? I hope so. If not, ask the teacher for notice by e-mail the same day when things like this take place, and have a discipline ready when he gets home from school. Yes, he and the other boy should have immediate consequences right there, from the teacher, but he also should come home to find that mom does indeed know what goes on at school and there is a consequence at home as well. the same day. Many younger elementary kids don't quite get yet that school and home are not totally separate worlds; they need to learn that parents do find out what they're doing at school. He is now old enough to grasp the idea that behaving a certain way at school also carries over to home.