McCain and Flake were among 10 Republicans who joined 54 Democrats to pass the Employment Non-Discrimination Act on a 64-32 vote that gay-rights groups called a historic development in civil rights.
As recently as Monday, Flake had opposed the bill in a procedural vote. But he reversed his stance Thursday and supported the measure, citing his earlier support of a similar bill when he was a member of the House.
"As I said in 2007 when I voted for the Employment Non-Discrimination Act in the House, one of the most important constitutionally mandated functions of the federal government is to protect the rights of individuals," Flake said in a statement after Thursday's vote. "While I had concerns about expanding protections beyond those House provisions, after consideration I believe supporting this bill is the right thing to do."
McCain, who cosponsored a successful amendment to the bill that exempts religious organizations from the law, said he was happy to support the bill with that limitation.
"I have always believed that workplace discrimination - whether based on religion, gender, race, national origin or sexual orientation - is inconsistent with the basic values that America holds dear," McCain said in a prepared statement.
While it passed the Senate comfortably and has been endorsed by President Barack Obama, who promised to sign the bill should it reach his desk, its chances in the House are less promising.
House Speaker John Boehner has said he does not support the bill, saying it opens the door for frivolous lawsuits, especially against small businesses.
Still, advocacy groups hailed Thursday's vote. Chad Griffin, president of the gay-rights advocacy group Human Rights Campaign, said in a statement Thursday that the Senate "made history" by passing the bill, which has been introduced in every session of Congress since the 103rd, in 1993-94, according to the Congressional Research Service.
Griffin said he was confident the bill would pass the House if it is brought up for a vote, and he criticized Boehner for "prematurely" voicing opposition to the bill.
"Each and every American worker should be judged based on the work they do, and never based on who they are," Griffin's statement said. "This broad Senate coalition has sent a vital message that civil-rights legislation should never be tied up by partisan political games."
But Boehner spokesman Michael Steel said in an emailed statement Thursday that Boehner continues to oppose the bill because of its potential economic impact.
"The speaker believes this legislation will increase frivolous litigation and cost American jobs, especially small business jobs," Steel said.
With the bill's fate still up in the air, Griffin urged Obama to sign an executive order protecting employees of federal contractors from discrimination based on their sexual orientation or gender identity. Such an order, while not as far-reaching as the bill, would still apply to about 16 million American workers, Griffin said.