Mass shootings near Johannesburg and Cape Town have left 27 people dead, South African police reported.